﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Xml;
using DataAccessLayer;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Forms;
using Npgsql;

namespace LibHuman
{
    
    public class AccessData
    {
        public const string Msg = "Ham Soft Human Resource Management";
        private clsDAL objDAL;
        private clsDAL DAL;
        DataSet ds = new DataSet();
        public const string hrs_userid = "ksv", hrs_pass = "ksv0507";
        public const string s_links_userid = "-99999";        
        public bool connected = true;
        string sConn = "Server=localhost;Port=5432;User Id=medisoft;Password=links1920;Database=human;Encoding=UNICODE;Pooling=true;";
        string owner = "medisoft", password = "links1920", userid = "nhansu",user_payrollid="luong", database = "human", sql = "", sqlUpd = "", sqlIns="";
        public AccessData()
        {
            if (Maincode("User") != "") userid = Maincode("User");
            if (Maincode("UserID") != "") owner = Maincode("UserID");
            if (Maincode("Password") != "") password = Maincode("Password");
            if (Maincode("Database") != "") database = Maincode("Database");
            sConn = "Server=" + Maincode("Ip") + ";Port=" + Maincode("Post") + ";User Id=" + owner + ";Password=" + password + ";Database=" + database + ";Encoding=UNICODE;Pooling=true;";
            objDAL = new clsDAL(sConn);
            DAL = new clsDAL(sConn);
            connected = objDAL.check_connect();
            if (!connected)
            {
                MessageBox.Show("Không thể kết nối với máy chủ!", Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }            
        }
        public string Maincode(string sql)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("..\\xml\\maincode.xml");
            XmlNodeList nodeLst = doc.GetElementsByTagName(sql);
            return nodeLst.Item(0).InnerText;
        }
        public string Thongso(string sql)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("..\\xml\\thongso.xml");
                XmlNodeList nodeLst = doc.GetElementsByTagName(sql);
                return nodeLst.Item(0).InnerText;
            }
            catch
            {
                ds = new DataSet();
                ds.ReadXml("..\\xml\\thongso.xml");
                DataColumn dc = new DataColumn();
                dc.ColumnName = sql;
                dc.DataType = Type.GetType("System.String");
                ds.Tables[0].Columns.Add(dc);
                ds.Tables[0].Rows[0][sql] = "";
                ds.WriteXml("..\\xml\\thongso.xml");
                return "";
            }
        }
        public string user { get { return userid; } }

        public string user_payroll { get { return user_payrollid; } }
        public string Matt()
        {
            return objDAL.getDataByCondition("select ten from "+user+".thongso where id=23").Tables[0].Rows[0][0].ToString();
        }
        public string Maqu()
        {
            DataSet ds = objDAL.getDataByCondition("select ten from "+ user+".thongso where id=9");
            return ds.Tables[0].Rows[0][0].ToString().Trim();
        }
        public string ten_tt()
        {
            DataSet ds = objDAL.getDataByCondition(" select tentt from "+user+".btdtt where matt=(select ten from "+user+".thongso where id=23)");
            return ds.Tables[0].Rows[0]["tentt"].ToString().Trim();
        }
        public string gio_cc(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select gbd_ccchieu from "+user+".heso where mmyy='"+s_mmyy.Trim()+"'");
            return ds.Tables[0].Rows[0]["gbd_ccchieu"].ToString().Trim();
        }
        public string nhap_macc()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=32");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string ngonngu()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=34");
            return ds.Tables[0].Rows[0]["ngonngu"].ToString().Trim();
        }
        public string cc_tructc(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select cctructc from " + user + ".heso where mmyy='"+s_mmyy.Trim()+"'");
            return ds.Tables[0].Rows[0]["cctructc"].ToString().Trim();
        }
        public string cc_thuongquy(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select ccthuongquy from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["ccthuongquy"].ToString().Trim();
        }
        public string abc_uni(string s)
        {
            DataSet ds = new DataSet();
            ds.ReadXml("xml\\doifont.xml");
            string s_abc = s.Trim(), s_uni = "", s1 = "óïñòô­øõö÷ùýúûüþãßáâä«èåæçé¬íêëìî1";
            DataRow r;
            for (int i = 0; i < s_abc.Length; i++)
            {
                if (s1.IndexOf(s_abc[i].ToString()) != -1)
                {
                    r = getrowbyid(ds.Tables[0], "abc1='" + s_abc[i].ToString() + "'");
                    if (r != null) s_uni += r[3].ToString();
                    else s_uni += s_abc[i];
                }
                else
                {
                    r = getrowbyid(ds.Tables[0], "abc='" + s_abc[i].ToString() + "'");
                    if (r != null) s_uni += r[1].ToString();
                    else s_uni += s_abc[i];
                }
            }
            return s_uni;
        }
        public string Syte()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=4");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string Tenbv { get { return Maincode("Tenbv"); } }
        public string Diachi()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=5");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string Dienthoai()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=6");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string Maso { get { return Maincode("Maso"); } }
        public string Giamdoc()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=14");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string Gdcongty()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=19");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string tp_tochuc()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=29");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string tyle_tv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select lg_tapsu from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["lg_tapsu"].ToString().Trim();
        }
        public string tyle_dh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select lg_dihoc from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["lg_dihoc"].ToString().Trim();
        }
        public string tyle_lths(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select lg_luutruhs from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["lg_luutruhs"].ToString().Trim();
        }
        public bool cc_cuoituan(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select cccuoituan from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["cccuoituan"].ToString().Trim()=="1";
        }
        public bool cc_ngaycn(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select cc_ngaycn from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0]["cc_ngaycn"].ToString().Trim())==1;
        }
        public string fax()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=28");
            return ds.Tables[0].Rows[0]["fax"].ToString().Trim();
        }
        public string website()
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".thongso where id=26");
            return ds.Tables[0].Rows[0]["website"].ToString().Trim();
        }
        public string phepthem(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select h_phepthem from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_phepthem"].ToString().Trim();
        }
        public int lan_ccvt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select lanccvt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0][0].ToString().Trim()!="" ? int.Parse(ds.Tables[0].Rows[0][0].ToString()): 3;
        }
        public bool bTheATM()
        {
            DataSet ds = objDAL.getDataByCondition("select ten from " + user + ".thongso where id=13");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public bool bVantay(string s_mmyy)
        {
            try
            {
                DataSet ds = objDAL.getDataByCondition("select vantay from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
                return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
            }
            catch
            {
                return false;
            }
        }
        public bool bthcc_tudong(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select thcctudong from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public bool bsd_tgsomtre(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select sd_tgsomtre from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public bool bcctc_taikhoa(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select cctc_taikhoa from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public bool sd_ccngayt7(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select sd_ccngayt7 from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public bool bdg_cbcc()
        {
            DataSet ds = objDAL.getDataByCondition("select ten from " + user + ".thongso where id=16");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        }
        public double pltoithieu()
        {
            DataSet ds = objDAL.getDataByCondition("select ten from " + user + ".thongso where id=12");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int khoadl_phep(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select khoadl_phep from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int luonghd(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select luonghd from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int hd_bantg(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select hd_bantg from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int luong_sp(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select luong_sp from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int ngachdn(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select ngachdn from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int tn_thcc(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select tn_thcc from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public int dn_thcc(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select dn_thcc from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public decimal ngaylv_thucte(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select snlvthucte from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return decimal .Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public decimal ngaylv_quydinh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition("select lamviec from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        //thong so cho phan he so phu cap
        public string h_hsvk(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsvk,1,1) as h_hsvk from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsvk"].ToString().Trim();
        }
        public string h_stvk(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsvk,2,1) as h_stvk from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stvk"].ToString().Trim();
        }
        public string h_tlvk(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsvk,3,1) as h_tlvk from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlvk"].ToString().Trim();
        }
        public string h_hscv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hscv,1,1) as h_hscv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hscv"].ToString().Trim();
        }
        public string h_stcv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hscv,2,1) as h_stcv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stcv"].ToString().Trim();
        }
        public string h_tlcv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hscv,3,1) as h_tlcv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlcv"].ToString().Trim();
        }

        public string h_hsdb(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdb,1,1) as h_hsdb from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsdb"].ToString().Trim();
        }
        public string h_stdb(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdb,2,1) as h_stdb from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stdb"].ToString().Trim();
        }
        public string h_tldb(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdb,3,1) as h_tldb from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tldb"].ToString().Trim();
        }
        public string h_hsdt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdt,1,1) as h_hsdt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsdt"].ToString().Trim();
        }
        public string h_stdt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdt,2,1) as h_stdt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stdt"].ToString().Trim();
        }
        public string h_tldt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdt,3,1) as h_tldt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tldt"].ToString().Trim();
        }
        public string h_hsth(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsth,1,1) as h_hsth from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsth"].ToString().Trim();
        }
        public string h_stth(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsth,2,1) as h_stth from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stth"].ToString().Trim();
        }
        public string h_tlth(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsth,3,1) as h_tlth from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlth"].ToString().Trim();
        }

        public string h_hsld(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsld,1,1) as h_hsld from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsld"].ToString().Trim();
        }
        public string h_stld(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsld,2,1) as h_stld from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stld"].ToString().Trim();
        }
        public string h_tlld(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsld,3,1) as h_tlld from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlld"].ToString().Trim();
        }

        public string h_hsdh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdh,1,1) as h_hsdh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsdh"].ToString().Trim();
        }
        public string h_stdh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdh,2,1) as h_stdh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stdh"].ToString().Trim();
        }
        public string h_tldh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsdh,3,1) as h_tldh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tldh"].ToString().Trim();
        }

        public string h_hskv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskv,1,1) as h_hskv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hskv"].ToString().Trim();
        }
        public string h_stkv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskv,2,1) as h_stkv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stkv"].ToString().Trim();
        }
        public string h_tlkv(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskv,3,1) as h_tlkv from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlkv"].ToString().Trim();
        }
        public string h_hsud(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsud,1,1) as h_hsud from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsud"].ToString().Trim();
        }
        public string h_stud(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsud,2,1) as h_stud from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stud"].ToString().Trim();
        }
        public string h_tlud(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsud,3,1) as h_tlud from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlud"].ToString().Trim();
        }

        public string h_hstn(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstn,1,1) as h_hstn from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hstn"].ToString().Trim();
        }
        public string h_sttn(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstn,2,1) as h_sttn from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_sttn"].ToString().Trim();
        }
        public string h_tltn(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstn,3,1) as h_tltn from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tltn"].ToString().Trim();
        }

        public string h_hsta(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsta,1,1) as h_hsta from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hsta"].ToString().Trim();
        }
        public string h_stta(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsta,2,1) as h_stta from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stta"].ToString().Trim();
        }
        public string h_tlta(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hsta,3,1) as h_tlta from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlta"].ToString().Trim();
        }

        public string h_hstx(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstx,1,1) as h_hstx from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hstx"].ToString().Trim();
        }
        public string h_sttx(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstx,2,1) as h_sttx from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_sttx"].ToString().Trim();
        }
        public string h_tltx(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstx,3,1) as h_tltx from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tltx"].ToString().Trim();
        }
        public string h_hstdt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstdt,1,1) as h_hstdt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hstdt"].ToString().Trim();
        }
        public string h_sttdt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstdt,2,1) as h_sttdt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_sttdt"].ToString().Trim();
        }
        public string h_tltdt(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hstdt,3,1) as h_tltdt from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tltdt"].ToString().Trim();
        }
        public string h_hskh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskh,1,1) as h_hskh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_hskh"].ToString().Trim();
        }
        public string h_stkh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskh,2,1) as h_stkh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_stkh"].ToString().Trim();
        }
        public string h_tlkh(string s_mmyy)
        {
            DataSet ds = objDAL.getDataByCondition(" select substring(h_hskh,3,1) as h_tlkh from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'");
            return ds.Tables[0].Rows[0]["h_tlkh"].ToString().Trim();
        }
        public string get_tennvdangnhap(string user_dn)
        {
            DataSet ds = objDAL.getDataByCondition(" select ten from " + user + ".pwhuman where username='" + user_dn.Trim() + "'");
            return ds.Tables[0].Rows[0]["ten"].ToString().Trim();
        }
        public string Mabv
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("..\\xml\\maincode.xml");
                XmlNodeList nodeLst = doc.GetElementsByTagName("Mabv");
                return (nodeLst.Item(0).InnerText == "") ? "701.1.01" : nodeLst.Item(0).InnerText;
            }
        }
        public bool kiemtrangayhople(string ngay)
        {
            try
            {
                if (ngay.IndexOf("_") != -1 || (ngay.IndexOf(" ") != -1 && ngay.Trim().Length == 10)) return false;
                int len = ngay.Length;
                if (len == 0) return false;
                string dd = ngay.Substring(0, 2), mm = ngay.Substring(3, 2), yyyy = ngay.Substring(6, 4);
                string s31 = "01+03+05+07+08+10+12+", s30 = "04+06+09+11+", s2829 = (int.Parse(yyyy) % 4 == 0) ? "29" : "28";
                if (int.Parse(yyyy.Substring(0, 1)) < 1) return false;
                if (int.Parse(mm) < 1 || int.Parse(mm) > 12) return false;
                if (s31.IndexOf(mm + "+") > -1)
                {
                    if (int.Parse(dd) < 1 || int.Parse(dd) > 31) return false;
                }
                else if (s30.IndexOf(mm + "+") > -1)
                {
                    if (int.Parse(dd) < 1 || int.Parse(dd) > 30) return false;
                }
                else if (int.Parse(dd) < 1 || int.Parse(dd) > int.Parse(s2829)) return false;
                if (len > 10)
                {
                    string hh = ngay.Substring(11, 2), MM = ngay.Substring(14, 2);
                    if (int.Parse(hh) > 23) return false;
                    if (int.Parse(MM) > 59) return false;
                }
                return true;
            }
            catch { return false; };
        }
        
        #region Ngach cong chuc
        public bool Insert_DMNGACHCONGCHUC(string ma, string ten, decimal n1, decimal n2, decimal n3, decimal n4, decimal n5,
        decimal n6, decimal n7, decimal n8, decimal n9, decimal n10, decimal n11, decimal n12, string ma1, string loai,string nhomngach,decimal stt)
        {
            string sqlIns = "", sqlUpd = "";
            int i = 0;
            sql = "UPDATE " + user + ".mangach set diengiai='" + ten + "',";
            sql += " b1=" + n1 + ",b2=" + n2 + ",b3=" + n3 + ",b4=" + n4 + ",";
            sql += " b5=" + n5 + ",b6=" + n6 + ",b7=" + n7 + ",b8=" + n8 + ",";
            sql += "b9=" + n9 + ",b10=" + n10 + ",b11=" + n11 + ",b12=" + n12 + ",";
            sql += "ma1='" + ma1.Trim() + "',loaingach='" + loai + "',nhomngach='"+nhomngach+"',stt="+stt+"";
            sql += " where ma='" + ma + "'";
            try
            {
                i = objDAL.Upd_data(sql);
                if (i == 0)
                {
                    sqlIns = "INSERT INTO " + user + ".mangach(ma,diengiai,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,ma1,loaingach,nhomngach,stt) ";
                    sqlIns += " VALUES ('" + ma.Trim() + "','" + ten.Trim() + "'," + n1 + "," + n2 + "," + n3 + "," + n4 + "," + n5 + "," + n6 + "";
                    sqlIns += " ," + n7 + "," + n8 + "," + n9 + "," + n10 + "," + n11 + "," + n12 + ",'" + ma1.Trim() + "','" + loai.Trim() + "','" + nhomngach + "',"+stt+")";
                    i = objDAL.Upd_data(sqlIns);
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion
        public DataSet get_data(string cmdText)
        {
            return objDAL.getDataByCondition(cmdText);
        }

        public DataSet get_data(string cmdText,string cmdText1)
        {
            return objDAL.getDataByCondition(cmdText,cmdText1);
        }
        public DataSet get_data(string cmdText, string cmdText1, string cmdText2)
        {
            return objDAL.getDataByCondition(cmdText, cmdText1, cmdText2);
        }
        public bool Execute_data(string cmdText)
        {
            return (objDAL.Upd_data(cmdText)>0);
        }
        public void delrec(DataTable dt, string exp)
        {
            try
            {
                DataRow[] r = dt.Select(exp);
                for (int i = 0; i < r.Length; i++) r[i].Delete();
            }
            catch { }
        }

        public DataRow getrowbyid(DataTable dt, string exp)
        {
            try
            {
                DataRow[] r = dt.Select(exp);
                return r[0];
            }
            catch { return null; }
        }
        public bool TestTime(TextBox txt)
        {
            bool kt = true;

            if (txt.Text.Trim().Length == 5 && txt.Text.Trim().Substring(2, 1) != ":")
            {
                kt = false;
            }
            if (txt.Text.Trim().Length < 4)
            {
                kt = false;
            }
            try
            {
                if (txt.Text.Trim().Length < 5)
                {
                    int t1 = int.Parse(txt.Text.Trim().Substring(0, 2));
                    int t2 = int.Parse(txt.Text.Trim().Substring(2, 2));
                    if (t1 > 24 || t2 > 59)
                        kt = false;
                }
            }
            catch
            { kt = false; }
            return kt;
        }
        public void FormatTime(TextBox txt)
        {
            if (txt.Text.Trim().Length == 4)
            {
                txt.Text = txt.Text.Substring(0, 2) + ":" + txt.Text.Substring(2, 2);
            }
        }
        public bool IsNumeric(string strNum)
        {
            int iNum;
            try
            {
                iNum = int.Parse(strNum);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #region pwhuman
        public bool upd_pwhuman( string m_userid,string m_hoten, string m_password, string m_right,string m_makp,bool mAdmin)
        {
            int i = 0;
            sql = "update " + user + ".pwhuman set ten='"+m_hoten+"',username='"+m_userid+"',pass='"+m_password+"'";
            sql += ",_right='"+m_right+"',_admin="+mAdmin+",makp='"+m_makp+"'";
            sql += " where username='"+m_userid+"'";
            i = objDAL.Upd_data(sql);
            if (i == 0)
            {
                sql = "insert into " + user + ".pwhuman(username,pass,_right";
                sql += ",ten,_admin,makp";
                sql += ") values ";
                sql += "('" + m_userid + "','" + m_password + "','" + m_right + "','" + m_hoten + "',false,";
                sql += "'"+m_makp+"')";
                i = objDAL.Upd_data(sql);
            }
            return (i>0);
        }
        public bool upd_pwhuman(string m_id, string m_right)
        {
            sql = "update " + user + ".pwhuman set _right='" + m_right + "' where username='"+m_id+"'";
            int i = 0;
            try
            {
                i=objDAL.Upd_data(sql);
            }
            catch 
            {
                return false;
            }
            return true;
        }
        #endregion
        public void updrec(DataTable dt, string m_userid, string password, string right)
        {
            string exp = "username='" + m_userid+"'";
            DataRow r = getrowbyid(dt, exp);
            if (r == null)
            {
                DataRow nrow = dt.NewRow();
                nrow[1] = userid;
                nrow[2] = password;
                nrow[3] = right;
                dt.Rows.Add(nrow);
            }
            else
            {
                DataRow[] dr = dt.Select(exp);
                dr[0][3] = right;
            }
        }
        public void setStandar()
        {
            try
            {
                string path = "Control Panel\\International";
                RegistryKey k = Registry.CurrentUser;//chỉ ảnh hưởng user hiện tại
                k = k.OpenSubKey(path, true);
                k.SetValue("sShortDate", "MM/dd/yyyy");//ngày kiểu shortdate
                k.SetValue("sLongDate", "dddd, MMMM dd, yyyy");//ngày kiểu longdate
                k.SetValue("sDecimal", ".");//giá trị thập phân
                k.SetValue("sThousand", ",");//dấu nhóm chữ số 1,000,000
                k.SetValue("sNegativeSign", "-");//ký hiệu âm
                k.SetValue("iLZero", "0");// 1:có '0' đứng trước 0.7; 0 ngược lại

                k.SetValue("sTime", ":");//dấu cách giữ hh:mm:ss
                k.SetValue("sDate", "/");//dấu cách giữ MM/dd/yyyy
                k.SetValue("sTimeFormat", "hh:mm:ss tt");//định định dạng thời gian
                k.SetValue("s1159", "AM");//buổi sáng
                k.SetValue("s2359", "PM");//buổi chiều

                k.Flush();
            }
            catch
            { }
        }
        #region Danh muc
        public bool Insert_DMBOPHAN(string ma, string ten, string a,decimal sott,string sogiuong,string khuvuc)
        {
            string sqlIns = "INSERT INTO " + user + ".bophan(madv,tenbp,mabsoft,sothutu,sogiuong,khuvuc) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "','" + a + "'," + sott + ",'" + sogiuong + "','"+khuvuc+"')";
            string sqlUpd = "UPDATE " + user + ".bophan SET tenbp='" + ten.Trim() + "',mabsoft='" + a + "',sothutu=" + sott + ",sogiuong='" + sogiuong + "',khuvuc='"+khuvuc+"' WHERE madv='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i=objDAL.Upd_data(sqlIns);
            }
            return (i>0);
        }
        public bool Insert_DMCHUCVU(string ma, string ten,string tenvt,decimal  sott,decimal heso,bool lanhdao)
        {                 
            tenvt = tenvt.Trim() != "" ? tenvt : ten;
            string sqlIns = "INSERT INTO " + user + ".dmcv(macv,diengiai,tenvt,stt,hscv,lanhdao) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "','" + tenvt + "'," + sott + "," + heso + "," + lanhdao + ")";
            string sqlUpd = "UPDATE " + user + ".dmcv SET diengiai='" + ten.Trim() + "',stt=" + sott + ",tenvt='" + tenvt + "',hscv=" + heso + ",lanhdao=" + lanhdao + " WHERE macv='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        public bool Insert_DMBENHVIEN(string ma, string ten)
        {
            string sqlIns = "INSERT INTO " + user + ".benhvien(mabv,tenbv) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".benhvien SET tenbv='" + ten.Trim() + "' WHERE mabv='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        public bool Insert_DMDANTOC(string ma, string ten)
        {
            string sqlIns = "INSERT INTO " + user + ".dantoc(madt,tendt) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".dantoc SET tendt='" + ten.Trim() + "' WHERE madt='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        #endregion
        public DataSet get_llnv(string m_bp)
        {
            string sql = "";
            sql = "select a.manv,trim(a.ho)||' '||trim(a.ten) as hovaten,b.tenbp,d.diengiai as chucvu ";
            sql += " ,trim(a.noio)||', '||trim(tt.tentt)||', '||trim(qu.tenquan) as noio";
            sql += " from " + user + ".llnv a inner join " + user + ".hopdong c on a.manv=c.manv  ";
            sql += " left join " + user + ".bophan b on a.madv=b.madv ";
            sql += " left join " + user + ".dmcv d on c.macv=d.macv";
            sql += " left join "+user+".btdtt tt on a.t_noio=tt.matt";
            sql += " left join "+user+".btdquan qu on a.h_noio=qu.maqu";
            sql += " inner join " + user + ".max_lan ml on c.manv=ml.manv";
            sql += " where (c.ngaynghi is null or to_timestamp(c.ngaynghi,'yyyy-mm-dd') > to_timestamp(now(),'yyyy-MM-dd'))  and c.lan=ml.lan ";
            if (m_bp != "")
            sql += " and a.madv='" + m_bp + "'";
            sql += " order by a.manv "; 
         
            return objDAL.getDataByCondition(sql);
        }
    
        public DataSet LyLichNV(string ma_nv)
        {            
            string sql = "";
            sql = "select mnvmay,manv,madv,ho,ten,phai,bidanh,stt,msthue,";
            sql += " madt,matgiao,to_char(ngaysinh,'dd/mm/yyyy') as ngaysinh,";
            sql += " quequan,maqg,noisinh,dienthoai,diachi,t_diachi,h_diachi,";
            sql += " noio,t_noio,h_noio,cmndso,noicapcmnd,";
            sql += " to_char(cmndngay,'dd/mm/yyyy') as ngaycapcmnd,";
            sql += " mahv,mann,mann1,mann2,mann3,tdth,tdqlnn,chinhtri,nnghiep,hinhanh,sothich,kynang,";
            sql += " sold,to_char(ngaysold,'dd/mm/yyyy') as ngay_sold,"; //ngaysold ngày sổ lao động
            sql += " huyensld,tinhsld,dkldso,";
            sql += " to_char(ngaydkld,'dd/mm/yyyy') as ngay_dkld,";
            sql += " to_char(ngaycap,'dd/mm/yyyy') as ngay_cap,matinhbhxh,mavt,namsinh,dtdd,macd,sotk,nganhang,ip,macc";
            sql += " from " + user + ".llnv where mnvmay ='" + ma_nv.Trim() + "'  order by manv";
            return objDAL.getDataByCondition(sql);
        }
        public DataSet LyLichNVhd(string ma_nv)
        {
            sql = "SELECT manvmay,manv,macv," +
                " to_char(NGAYDENCT,'dd/mm/yyyy') AS m_ngaydenct," +
                " dang,chi_bo,doan,cong_doan,nhandang,to_char(ngaybn,'dd/mm/yyyy') as m_ngaybn,tgbn,tdqlnn" +
                " FROM " + user + ".hopdong WHERE manv='" + ma_nv.Trim() + "'";
            return objDAL.getDataByCondition(sql);
        }
        public decimal next_bacluong(string ma, decimal hslc)
        {
            DataTable dt = objDAL.getDataByCondition("select * from "+user+".mangach").Tables[0];
            int r = 0, c = 2;
            bool bFound = false;
            for (r = 0; r < dt.Rows.Count; r++)
            {
                if (dt.Rows[r]["ma"].ToString() == ma)
                {
                    for (c = 2; c < 13; c++)
                        if (decimal.Parse(dt.Rows[r][c].ToString()) == hslc)
                        {
                            bFound = true;
                            break;
                        }
                    break;
                }
            }
            return (bFound) ? (c - 1) : 0;
        }
        public bool upd_barcode(string m_manv,byte[] i_mage)
        {
            return objDAL.upd_llnv_barcode(user, m_manv, i_mage);
        }
        public bool upd_image_nv(string m_manv,byte[] i_mage)
        {
            return objDAL.upd_llnv_image(user, m_manv, i_mage);
        }
        public bool upd_lydonghiphep(string ma, string ten, string nhombc, decimal stt, string kihieu)
        {
            return objDAL.upd_nghiphep(ma, ten, nhombc, stt, kihieu, user);
        }
        public System.DateTime StringToDate(string s)
        {
            s = (s == "" || s == null) ? objDAL.ngayhienhanh_server.Substring(0, 10) : s;
            string[] format ={ "dd/MM/yyyy" };
            return System.DateTime.ParseExact(s.Substring(0, 10), format, System.Globalization.DateTimeFormatInfo.CurrentInfo, System.Globalization.DateTimeStyles.None);
        }

        public string StringToMMDDYYYY(string s)
        {
            return s.Substring(3, 2) + "/" + s.Substring(0, 2) + "/" + s.Substring(6, 4);
        }

        public string StringToDDMMYYYY(string s)
        {
            return s.Substring(0, 2) + s.Substring(3, 2) + s.Substring(6, 4);
        }

        public System.DateTime StringToDateTime(string s)
        {
            string[] format1 ={ "dd/MM/yyyy" }, format2 ={ "dd/MM/yyyy HH:mm" };
            return System.DateTime.ParseExact(s.ToString(), (s.Length == 10) ? format1 : format2, System.Globalization.DateTimeFormatInfo.CurrentInfo, System.Globalization.DateTimeStyles.None);
        }

        public System.DateTime StringToDateTime(string s, string f)
        {
            string[] format ={ f };
            return System.DateTime.ParseExact(s.ToString(), format, System.Globalization.DateTimeFormatInfo.CurrentInfo, System.Globalization.DateTimeStyles.None);
        }

        public string DateToString(string format, System.DateTime date)
        {
            if (date.Equals(null)) return "";
            else return date.ToString(format, System.Globalization.DateTimeFormatInfo.CurrentInfo);
        }
        public bool Insert_hopdongLylich(string ma_nv, string bo_phan,
            string manv1, string sott, string chucvu, string cthich_cv, string ngay_bn, int tg_bn,decimal hs_cv)
        {
             return objDAL.Insert_hopdongLylich(ma_nv, bo_phan, manv1, sott, chucvu, cthich_cv, ngay_bn, tg_bn,hs_cv, user );
        }
        public bool Insert_lylichnv(string ma_nv, string manv1, string bo_phan,
            string sott,  string ho_nv, string ten_nv,
            string bi_danh, string ngay_sinh, string phai_,
            string noi_sinh, string nguyen_quan, string noi_thuongtru,
            string tinh_thuongtru, string huyen_thuongtru,
            string noi_ohiennay, string tinh_ohiennay,
            string huyen_ohiennay, string dien_thoai,
            string so_cmnd, string ngay_capcmnd, string noi_capcmnd,
            string dan_toc, string ton_giao, string quoc_tich, string s_mavt,
            int s_nams, string s_dtdd, string s_macd,string s_sotk,string s_nganhang,string msthue,string ten_nd,string macc, string ngayud )
        {
            return objDAL.Insert_lylichnv(ma_nv, manv1, bo_phan,
             sott, ho_nv, ten_nv,
             bi_danh, ngay_sinh, phai_,
             noi_sinh, nguyen_quan, noi_thuongtru,
            tinh_thuongtru, huyen_thuongtru,
             noi_ohiennay, tinh_ohiennay,
             huyen_ohiennay, dien_thoai,
             so_cmnd, ngay_capcmnd, noi_capcmnd,
             dan_toc, ton_giao, quoc_tich, s_mavt,
             s_nams, s_dtdd, s_macd,s_sotk,s_nganhang,msthue,ten_nd,macc,ngayud,user);
        }
        //public bool bMavach
        //{
        //    get
        //    {
        //        try
        //        {
        //            DataSet ds = objDAL.getDataByCondition("select ten from " + user + ".thongso where id=10");
        //            return int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 1;
        //        }
        //        catch
        //        {
        //            return false;
        //        }
        //    }
        //}
        public bool upd_table(string s_mmyy,string m_userid)
        {
            return objDAL.upd_table(s_mmyy, m_userid, user);
        }
        public void taotable_hesonv(string s_mmyy,string m_userid)
        {
            sql = "CREATE TABLE " + user + ".heso_nv_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_heso_nv_" + s_mmyy + " PRIMARY KEY (manv,madv,mmyy) ) INHERITS (" + user + ".heso_nv); ";
            objDAL.Upd_data(sql);           
        }
        public void taotable_thchamcong(string s_mmyy, string m_userid)
        {
            sql = "CREATE TABLE " + user + ".th_chamcong_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_th_chamcong_" + s_mmyy + " PRIMARY KEY (manv,madv,mmyy) ) INHERITS (" + user + ".th_chamcong); ";
            objDAL.Upd_data(sql);
        }
        public void taotable(string s_mmyy,string m_userid)
        {            
            sql = "CREATE TABLE "+user+".chamcong_"+s_mmyy+"  ( CHECK ( mmyy='" +s_mmyy+ "'), ";
            sql+=" CONSTRAINT pk_chamcong_"+s_mmyy+" PRIMARY KEY (manv,mmyy) ) INHERITS ("+user+".chamcong); ";
            objDAL.Upd_data(sql) ;
        }

        public void taotable_thuongtruc(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".chamcong_thuongtruc_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT chamcong_thuongtruc_pkey_" + s_mmyy + " PRIMARY KEY (manv,mmyy) ) INHERITS (" + user + ".chamcong_thuongtruc); ";
            objDAL.Upd_data(sql);
        }

        public void taotable_ngoaigio(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".ccng_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_ccng_" + s_mmyy + " PRIMARY KEY (manv,mmyy,id) ) INHERITS (" + user + ".ccng); ";
            objDAL.Upd_data(sql);
        }
        //tao bang nhan vien lam thuong quy
        public void taotable_thuongquy(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".chamcong_thuongquy_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_chamcong_thuongquy_" + s_mmyy + " PRIMARY KEY (manv,mmyy,id) ) INHERITS (" + user + ".chamcong_thuongquy); ";
            objDAL.Upd_data(sql);
        }
        public void taotable_nvngoaigio(string s_mmyy)
        {

            sql = "CREATE TABLE " + user + ".ngoaigio_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_ngoaigio_" + s_mmyy + " PRIMARY KEY (manv,mmyy) ) INHERITS (" + user + ".ngoaigio); ";
            objDAL.Upd_data(sql);
        }
        //tao nhan vien lam thuong quy
        public void taotable_nvthuongquy(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".thuongquy_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_thuongquy_" + s_mmyy + " PRIMARY KEY (manv,mmyy) ) INHERITS (" + user + ".thuongquy); ";
            objDAL.Upd_data(sql);
        }
        // Ham tao bang cham cong vao
        public void taotable_chamcongvao(string s_mmyy)
        { 
            sql="CREATE TABLE " + user + ".ccvao_" + s_mmyy + " (CHECK (mmyy='" + s_mmyy + "'), ";
            sql += "CONSTRAINT pk_ccvao_" + s_mmyy + " PRIMARY KEY (manv,ngay,maca,kt_ng) ) INHERITS (" + user + ".ccvao); ";
            objDAL.Upd_data(sql);
        }
        // Ham tao bang cham cong ra
        public void taotable_chamcongra(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".ccra_" + s_mmyy + " (CHECK (mmyy='" + s_mmyy + "'), ";
            sql += "CONSTRAINT pk_ccra_" + s_mmyy + " PRIMARY KEY (manv,ngay,maca,kt_ng) ) INHERITS (" + user + ".ccra); ";
            objDAL.Upd_data(sql);
        }
        // Ham tao bang cham cong nghi
        public void taotable_chamcongnghi(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".ccnghi_" + s_mmyy + " (CHECK (mmyy='" + s_mmyy + "'), ";
            sql += "CONSTRAINT pk_ccnghi_" + s_mmyy + " PRIMARY KEY (manv,ngay,maca) ) INHERITS (" + user + ".ccnghi); ";
            objDAL.Upd_data(sql);
        }
        public void taotable_tangcuong(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".nv_tangcuong_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT pk_nv_tangcuong_" + s_mmyy + " PRIMARY KEY (manv,mmyy) ) INHERITS (" + user + ".nv_tangcuong); ";
            objDAL.Upd_data(sql);
        }
        public void taotable_tructc(string s_mmyy)
        {
            sql = "CREATE TABLE " + user + ".chamcong_tructc_" + s_mmyy + "  ( CHECK ( mmyy='" + s_mmyy + "'), ";
            sql += " CONSTRAINT chamcong_tructc_pkey_" + s_mmyy + " PRIMARY KEY (manv,mmyy) ) INHERITS (" + user + ".chamcong_tructc); ";
            objDAL.Upd_data(sql);
        }
        public void taotable_phep(string s_yyyy, string m_userid)
        {

            sql = "CREATE TABLE " + user + ".phepnam_" + s_yyyy + "  ( CHECK ( nam='" + s_yyyy + "'), ";
            sql += " CONSTRAINT pk_phepnam_" + s_yyyy + " PRIMARY KEY (manv,nam) ) INHERITS (" + user + ".phepnam); ";
            objDAL.Upd_data(sql);

            //sql = "CREATE OR REPLACE RULE phepnam_insert_" + s_yyyy + " AS ON INSERT TO " + user + ".phepnam ";
            //sql += "  where nam='" + s_yyyy + "' DO INSTEAD INSERT INTO " + user + ".phepnam_" + s_yyyy;
            //sql += " (manv,madv,phep,phepthem,nam,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,ts_ngayton) ";
            //sql += "  VALUES (NEW.manv,NEW.madv,NEW.phep,NEW.phepthem,NEW.nam,NEW.t1,NEW.t2,NEW.t3,NEW.t4,NEW.t5,NEW.t6,NEW.t7,NEW.t8,NEW.t9,NEW.t10,NEW.t11,NEW.t12,NEW.ts_ngayton)";
            //objDAL.Upd_data(sql);
            objDAL.Upd_data("insert into "+user+".nam values('"+s_yyyy+"')");
        }
        public string mm_truoc(string d_mmyy)
        {
            int t_mm;
            string mm = d_mmyy.Substring(0, 2), yy = d_mmyy.Substring(2, 2);
            if (mm == "01")
            {
                t_mm = 12;
            }
            else
            {
                t_mm = int.Parse(mm) - 1;
            }
            return t_mm.ToString();
        }
        public string yyyy_truoc(string yyyy)
        {
            int n_mm;
            n_mm = int.Parse(yyyy) - 1;

            return n_mm.ToString();
        }
        public bool bMmyy(string m_mmyy)
        {
            return get_data("select * from " + user + ".hr_table where mmyy='" + ((m_mmyy.Trim().Length == 4) ? m_mmyy : mmyy(m_mmyy)) + "'").Tables[0].Rows.Count > 0;
        }
        public bool byyyy(string m_yyyy)
        {
            return get_data("select * from " + user + ".nam where yyyy='" + m_yyyy.Trim()+ "'").Tables[0].Rows.Count > 0;
        }
        public string Mmyy_truoc(string d_mmyy)
        {
            int t_mm, t_yy;
            string mm = d_mmyy.Substring(0, 2), yy = d_mmyy.Substring(2, 2);
            if (mm == "01")
            {
                t_mm = 12; t_yy = int.Parse(yy) - 1;
            }
            else
            {
                t_mm = int.Parse(mm) - 1; t_yy = int.Parse(yy);
            }
            return t_mm.ToString().PadLeft(2, '0') + t_yy.ToString().PadLeft(2, '0');
        }

        public string Mmyy_sau(string d_mmyy)
        {
            int t_mm, t_yy;
            string mm = d_mmyy.Substring(0, 2), yy = d_mmyy.Substring(2, 2);
            if (mm == "12")
            {
                t_mm = 01; t_yy = int.Parse(yy) + 1;
            }
            else
            {
                t_mm = int.Parse(mm) + 1; t_yy = int.Parse(yy);
            }
            return t_mm.ToString().PadLeft(2, '0') + t_yy.ToString().PadLeft(2, '0');
        }
        public string mmyy(string ngay)
        {
            return ngay.Substring(3, 2) + ngay.Substring(8, 2);
        }
        public bool upd_btdtt(string stt, string m_matt, string m_tentt)
        {
            return objDAL.upd_btdtt(stt, m_matt, m_tentt, user);
        }
        public bool upd_btdquan(string m_matt, string m_maqu, string m_tenquan)
        {
            return objDAL.upd_btdquan(m_matt, m_maqu, m_tenquan, user);
        }
        public bool upd_btdpxa(string m_maqu, string m_maphuongxa, string m_tenpxa)
        {
            return objDAL.upd_btdpxa(m_maqu, m_maphuongxa, m_tenpxa, user);
        }
        public bool Insert_DMCHUCDANH(string ma, string ten)
        {
            string sqlIns = "INSERT INTO "+user+".dmcd(ma,ten) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".dmcd SET ten='" + ten.Trim() + "' WHERE ma='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);

        }
        public bool Insert_quocgia(string ma, string ten)
        {
            string sqlIns = "INSERT INTO " + user + ".quocgia(maqg,tenqg) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".quocgia SET tenqg='" + ten.Trim() + "' WHERE maqg='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);

        }
        public bool Insert_CTDAOTAO(string ma, string ten, string manhom)
        {
            string sqlIns = "INSERT INTO " + user + ".ctdaotao(mactdt,tenctdt,manhom) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "','" + manhom.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".ctdaotao SET tenctdt='" + ten.Trim() + "',manhom='" + manhom.Trim() + "' WHERE mactdt='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        //lay so le
        public int d_soluong_le(int d_nhom)
        {
            ds = get_data("select ten from " + user + ".thongso where id=57");
            if (ds.Tables[0].Rows.Count == 0) return 2;
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
     
        public int d_thanhtien_le(int d_nhom)
        {
            ds = get_data("select ten from " + user + ".thongso where id=59 ");
            if (ds.Tables[0].Rows.Count == 0) return 2;
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        public void check_process_Excel()
        {
            Process[] processes = Process.GetProcesses();

            if (processes.Length > 1)
            {
                int i = 0;
                for (int n = 0; n <= processes.Length - 1; n++)
                {
                    if (((Process)processes[n]).ProcessName == "EXCEL")
                    {
                        i++;
                        ((Process)processes[n]).Kill();
                    }
                }
            }
        }
        public string Export_Excel(DataTable dt, string tenfile)
        {
            try
            {
                string dirPath = AppDomain.CurrentDomain.BaseDirectory + "Excel";
                string filePath = dirPath + "\\" + tenfile + ".xls";
                if (!System.IO.Directory.Exists(dirPath)) System.IO.Directory.CreateDirectory(dirPath);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false, System.Text.Encoding.Unicode);
                string astr = "";
                astr = "<Table>";//"<Table border=1>";
                astr = astr + "<tr>";
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    astr = astr + "<th>";
                    astr = astr + dt.Columns[i].ColumnName;
                    astr = astr + "</th>";
                }
                astr = astr + "</tr>";
                sw.Write(astr);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    astr = "<tr>";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        astr = astr + "<td>";
                        astr = astr + dt.Rows[i][j].ToString();
                        astr = astr + "</td>";
                    }
                    astr = astr + "</tr>";
                    sw.Write(astr);
                }
                astr = "</Table>";
                sw.Write(astr);
                sw.Close();
                return filePath;
            }
            catch (Exception ex)
            {
                //upd_error(ex.Message, sComputer, tenfile);
                System.Windows.Forms.MessageBox.Show(ex.ToString());                   
                return "";
            }
        }        
       
        public DataSet get_hopdong(string ma_nv)
        {
            sql = "SELECT a.MANV,a.MADV,";
            sql += " a.MANVMAY, to_char(a.NGAYDENCT,'dd/mm/yyyy') as m_NGAYDENCT, a.HDLDSO,";
            sql += " a.LOAIHD, a.THOIGIAN, a.MANGACH,b.tenhd as diengiai,";
            sql += " to_char(a.TUNGAY,'dd/mm/yyyy') as m_TUNGAY,";
            sql += " to_char(a.DENNGAY,'dd/mm/yyyy') as m_DENNGAY,";
            sql += " to_char(a.S_BEGIN,'dd/mm/yyyy') as m_S_BEGIN,";
            sql += " to_char(a.NGAYHD,'dd/mm/yyyy') as m_NGAYHD,";
            sql += " a.LUONGHD, a.LUONGCT,lhd_bantg,st_bantg,luong_sp,pc_tdt,tongluong,";
            sql += " to_char(a.NGAYBHXH,'dd/mm/yyyy') as m_NGAYBHXH,";
            sql += " a.SONPHEP, a.BHXH,a.bhtn,";
            sql += " a.BHYT, a.HS_LC, a.HS_CV,";
            sql += " a.HS_TN, a.HS_KV,a.pc_tx, a.pc_khac,a.sotien_hd,a.HS_DT,a.pc_ta,a.hs_ud,a.ghichuct,";
            sql += " a.HS_TH, a.HS_LD, a.HS_DB,";
            sql += " a.HS_DH,a.HS_VK,";
            sql += " to_char(a.NGAYNGHI,'dd/mm/yyyy') as m_NGAYNGHI,";
            sql += " a.HESO,to_char(a.CHUYENCT,'dd/mm/yyyy') as m_CHUYENCT,";
            sql += " to_char(a.NGUNGCT,'dd/mm/yyyy') as m_NGUNGCT,";
            sql += " to_char(a.NGAYVK,'dd/mm/yyyy') as ngay_vk,";
            sql += " a.SLD_TUDAU,to_char(a.SLD_NGAYDI,'dd/mm/yyyy') as m_SLD_NGAYDI,";
            sql += " a.sobhxh,a.tgbhxh,a.k_tinhbhxh,a.nhandang,a.matd,a.bac,a.magc,a.tbnghihuu,a.bangkhen,a.thangnangluong,";
            sql += " to_char(a.NGAYCTNN,'dd/mm/yyyy') as ngay_ctnn,a.lydonghi as dgnghiviec,";
            sql += " to_char(a.NGAYTD,'dd/mm/yyyy') as ngay_td,a.noi_chuyenden,"; //dg1,dg2,dg3,dg4,dg5,dg6,dg7,dg8,dg9,dg10,dg11
            sql += " a.phepthem,to_char(a.ngaybnngach,'dd/mm/yyyy') as m_ngaybnngach,to_char(a.ngay_tapsu,'dd/mm/yyy') as m_ngay_tapsu,";
            sql += " a.tg_tapsu,to_char(a.chinhthuc_lv,'dd/mm/yyyy') as m_chinhthuc_lv,a.luong_tapsu,a.loaingach,a.ko_hoanthanh,a.nam,";
            sql += " a.ghichuphep,a.loaihuongphep,a.thaisan,to_char(a.ngayphepnam,'dd/mm/yyyy') as m_ngayphepnam,";
            sql += " to_char(a.tvtungay,'dd/mm/yyyy') as TVTN,to_char(a.tvdenngay,'dd/mm/yyyy') as TVDN,a.SOBHXH,a.K_TINHBHXH,a.TGBHXH,a.K_TINHBHXH,a.NHANDANG,a.MATD,a.BAC,a.MAGC,a.luuy,a.lan,a.lydonghi as ld_nghiviec ";
            sql += " from " + user + ".hopdong a left join " + user + ".mahd b on a.loaihd=b.mahd";
            sql += " where a.manv='" + ma_nv.Trim() + "'  order by a.lan asc";//desc

            return objDAL.getDataByCondition(sql);
        }
        public bool Del_hopdong(string ma, int m_lan)
        {
            sql = "UPDATE "+user+".hopdong SET hdldso='',ngayhd=null,loaihd='',";
            sql += " tungay=null,denngay=null,thoigian=0,luonghd=0,ngaydenct=null,";
            sql += " ngaynghi=null,sonphep=0,ngaybhxh=null,s_begin=null,bhyt=0,";
            sql += " bhxh=0,hs_cv=0,hs_tn=0,hs_dh=0,hs_ld=0,hs_db=0,";
            sql += " hs_kv=0,hs_lc=0,hs_th=0,luongct=0,mangach='',heso='',";
            sql += " hs_dt=0,hs_dt1=0,hs_ud=0,luuy='' WHERE manv='" + ma + "' and lan=" + m_lan.ToString();
            return (objDAL.Upd_data(sql)>0);
        }
        public bool Upd_ktkl(string ma_nv, string ngay_, string nd_KT, string hinh_thuc, string vb_so, string ghi_chu,bool m_ktkl)
        {
            return objDAL.Upd_ktkl(ma_nv, ngay_, nd_KT, hinh_thuc, vb_so, ghi_chu,m_ktkl, user);
        }
        public bool upd_dmhscv(string ma_nv,
            decimal cv, string s_madv, string s_macv, decimal tn,
            decimal hs_dhhd1, decimal hs_dbhd1, decimal hs_ldhd1,
            decimal hs_thhd1, decimal hs_kvhd1, decimal hs_congtacphi,decimal hs_vkhd)
        {
            return objDAL.upd_dmhscv(ma_nv,cv, s_madv, s_macv, tn, hs_dhhd1,hs_dbhd1,hs_ldhd1,
             hs_thhd1,hs_kvhd1,hs_congtacphi,hs_vkhd,user);
        }
        public bool upd_tangbc(string ma_nv1, string s_madv1, string s_macv1, string bc_loaihd)
        {
            
            return objDAL.upd_tangbc( ma_nv1,  s_madv1,  s_macv1,  bc_loaihd,user);
        }
        public bool upd_dmhslc(string ma_nv1, decimal s_hs_lc, string s_madv1, string s_macv1, string s_mangach)
        {
            return objDAL.upd_dmhslc( ma_nv1,s_hs_lc, s_madv1, s_macv1, s_mangach,user);
        }

        public bool upd_hopdong(string ma_nv, string shd, string nnl, string lhd,
            string hlt, string dn, int tg, decimal mltt, string ndct,
            string nnv, decimal snp, string ntBHXH, string nhl, decimal mbhyt, int dbhxh,
            decimal cv, decimal tn, decimal dh, decimal ld, decimal pcdb, decimal pckv, decimal l,
            decimal th, decimal mlcb, string ncc, string dg, decimal dt, string nvk, decimal vk,
            string td, int b, string gc, decimal dt1, decimal ud, string luuy, int m_lan,string hs_bdhv, decimal congtacphi)
        {
            return objDAL.upd_hopdong(ma_nv,  shd,  nnl,  lhd,
             hlt,  dn,  tg,  mltt,  ndct,
             nnv,  snp,  ntBHXH,  nhl,  mbhyt, dbhxh,
             cv,  tn,  dh,  ld,  pcdb,  pckv,  l,
             th,  mlcb,  ncc,  dg,  dt,  nvk,  vk,
             td, b,  gc,  dt1,  ud,  luuy,  m_lan,hs_bdhv,congtacphi , user);
        }
        public int Get_lan_hd(string manv)
        {
            return objDAL.Get_lan_hd(manv, user);
        }
        public bool update_kqlydo(string ma_nv, string tn, string dn, decimal mltt, decimal hs_lc,
            decimal hs_vk, decimal hs_cv, decimal hs_tn, decimal hs_dh, decimal hs_dt, string gchu, string ma_ld)
        {
            return objDAL.update_kqlydo( ma_nv,  tn,  dn,  mltt,  hs_lc,
             hs_vk, hs_cv,  hs_tn,  hs_dh,  hs_dt,  gchu,  ma_ld,user);
        }
        public bool upd_dmlydo(string ma, string ten)
        {
            string sqlIns = "INSERT INTO "+user+".dmlydo(ma,ten) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "')";
            string sqlUpd = "UPDATE " + user + ".dmlydo SET ten='" + ten.Trim() + "' WHERE ma='" + ma.Trim() + "'";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        public void upd_thongso(int m_id, string m_ten)
        {
            string sql = "update "+user+".thongso set ten='" + m_ten + "' where id=" + m_id;
            objDAL.Upd_data(sql);
        }
        public DataSet get_hopdong_nv(string m_manv)
        {
            sql = "select loaihd,to_char(tungay,'dd/mm/yyyy') as tungay,to_char(denngay,'dd/mm/yyyy') as denngay, ";
            sql += " hs_lc,hs_cv,hs_vk,hs_tn,hs_dh,hs_dt, ";
            sql += " hs_dt1,hs_ud,hs_th,hs_ld,hs_db,hs_kv,congtacphi,ghichu from " + user + ".hopdong where trim(manv)='" + m_manv + "'";
            return objDAL.getDataByCondition(sql);
        }
        // moi chinh sua
        public bool upd_llnvdaotao(string ma_nv, string nn1, string nn2,
            string nn3, string nn4, string hv, string cm, string td1, string h,
            int t, string td2, string td3, string td4, string td5, string tdvt, string hh)
        {

            try
            {
                sql = "UPDATE " + user + ".llnv SET ";
                sql += " mann = '" + nn1.Trim() + "',";
                sql += " mann1='" + nn2.Trim() + "',";
                sql += " mann2='" + nn3.Trim() + "',";
                sql += " mann3='" + nn4.Trim() + "',";
                sql += " mahv='" + hv.Trim() + "',";
                sql += " nnghiep='" + cm.Trim() + "',";
                sql += " td_nnghiep ='" + td1.Trim() + "',";
                sql += " he_dt='" + h.Trim() + "',";
                sql += " tg_dt =" + t + ",";
                sql += " tdnn='" + td2.Trim() + "',";
                sql += " tdnn1='" + td3 + "',";
                sql += " tdnn2='" + td4.Trim() + "',";
                sql += " tdnn3='" + td5.Trim() + "',";
                sql += " tdth='" + tdvt.Trim() + "',";
                sql += " mahocham = '" + hh.Trim() + "'";
                sql += " WHERE manv='" + ma_nv + "'";
            }
            catch (Exception ex) { return false; }
            return (objDAL.Upd_data(sql) > 0);
        }

        public bool upd_llnvdaotao(string ma_nv, string nn1, string nn2,
            string nn3, string nn4, string hv, string cm,string namtn_cm, string td1, string h,
            int t, string td2, string td3, string td4, string td5, string tdvt, string hh)
        {
            
            try
            {
                sql = "UPDATE " + user + ".llnv SET ";
                sql += " mann = '" + nn1.Trim() + "',";
                sql += " mann1='" + nn2.Trim() + "',";
                sql += " mann2='" + nn3.Trim() + "',";
                sql += " mann3='" + nn4.Trim() + "',";
                sql += " mahv='" + hv.Trim() + "',";
                sql += " nnghiep='" + cm.Trim() + "',";
                sql += " td_nnghiep ='" + td1.Trim() + "',";
                sql += " he_dt='" + h.Trim() + "',";
                sql += " tg_dt =" + t + ",";
                sql += " tdnn='" + td2.Trim() + "',";
                sql += " tdnn1='" + td3 + "',";
                sql += " tdnn2='" + td4.Trim() + "',";
                sql += " tdnn3='" + td5.Trim() + "',";
                sql += " tdth='" + tdvt.Trim() + "',";
                sql += " mahocham = '" + hh.Trim() + "',";
                sql += "namtn_cm=to_timestamp('"+namtn_cm+"','yyyy-mm-dd')";
                sql += " WHERE manv='" + ma_nv + "'";
            }
            catch (Exception ex) { return false; }
            return (objDAL.Upd_data(sql)>0);
        }
        public bool upd_hopdong_daotao(string ma_nv, string qlnnndh, string tdql)
        {
            sqlUpd = "UPDATE " + user + ".hopdong SET " +
                " namqlnn='" + qlnnndh.Trim() + "',tdqlnn='" + tdql.Trim() + "' WHERE manv='" + ma_nv.Trim() + "'";

            sqlIns = "INSERT INTO " + user + ".hopdong(manv,namqlnn,tdqlnn)";
            sqlIns += " VALUES('" + ma_nv.Trim() + "','" + qlnnndh.Trim() + "','" + tdql.Trim() + "')";
            int i = 0;
            i = objDAL.Upd_data(sqlUpd);
            if (i == 0)
            {
                i = objDAL.Upd_data(sqlIns);
            }
            return (i > 0);
        }
        public bool upd_hphi(string manv_cq,
            string mahv_cq, string dot_tt_cq, string sohd_cq,
            string hocphi_cq, string ngayhp_cq, string g_chu, int v_thang, string v_tungay, string v_denngay, string v_tennganh)
        {
            return objDAL.upd_hphi(manv_cq,
             mahv_cq, dot_tt_cq,  sohd_cq,
             hocphi_cq,  ngayhp_cq,  g_chu,  v_thang,  v_tungay,  v_denngay,  v_tennganh,user);
        }

        // them truong id
        public bool upd_qtdaotao(string manv_dt, string mahv_dt, string mabangcap_dt,
            string hehocvan_dt, string thang_dt, string tungay_dt,string tu_nam,
            string denngay_dt,string den_nam ,string ghichu_dt, decimal id, string donvi,string ten_nd,string noidt,string user)
        {
            return objDAL.upd_qtdaotao(manv_dt, mahv_dt, mabangcap_dt,
             hehocvan_dt, thang_dt, tungay_dt,tu_nam,
             denngay_dt,den_nam, ghichu_dt, id, donvi,ten_nd,noidt, user);
        }
        public bool upd_hphi(string manv_cq,
            string mahv_cq, string dot_tt_cq, string sohd_cq,
            string hocphi_cq, string ngayhp_cq, string g_chu, int v_thang, string v_tungay, string v_denngay, string v_tennganh, decimal id, string donvicq)
        {
            return objDAL.upd_hphi(manv_cq,
             mahv_cq, dot_tt_cq, sohd_cq,
             hocphi_cq, ngayhp_cq, g_chu, v_thang, v_tungay, v_denngay, v_tennganh, id, donvicq, user);
        }
       
        public bool upd_boiduongnv(string manv_nv, string mactdt_nv, int tg_nv, string tungay_nv, string denngay_nv, string diadiem_nv)
        {
            return objDAL.upd_boiduongnv(manv_nv, mactdt_nv,  tg_nv, tungay_nv, denngay_nv,  diadiem_nv,user);
        }
        //chinh sua truong hop co them id
        public DataSet f_Daotao(string ma_nv)
        {
            sql = " SELECT l.mnvmay,l.mahv AS mahvll, l.nnghiep, l.td_nnghiep, l.he_dt, l.tg_dt, l.mahocham, ";
            sql += " l.mann, l.tdnn, l.mann1, l.tdnn1, h.NAMQLNN, l.mann2, l.tdnn2, l.mann3, l.tdnn3, ";
            sql += " l.tdth, v.manv, v.tenctdt, v.mabangcap, v.tenbang, v.hehocvan, v.tenhe, v.mahv,v.tunam,v.dennam,";
            sql += " v.thang, to_char(v.tungay,'dd/mm/yyyy') AS tungay, to_char(v.denngay,'dd/mm/yyyy') AS denngay, v.ghichu,v.id,v.donvi,v.noi_dt ";
            sql += " FROM (" + user + ".llnv l INNER JOIN " + user + ".hopdong h ON l.manv=h.manv) ";
            sql += " INNER JOIN " + user + ".v_qtdaotao v ON h.MANV=v.manv ";
            sql += " inner join " + user + ".max_lan ml on h.manv=ml.manv";
            sql += " where l.manv='" + ma_nv + "' and h.lan=ml.lan";
            return objDAL.getDataByCondition(sql);
        }
        public DataSet f_DaotaoCQ(string ma_nv)
        {
            string sql1 = "";
            sql1 = "SELECT h.manv, h.mahv, c.tenctdt, h.dot_tt, h.sohd, h.hocphi, h.ngayhp, h.thang, h.tungay, h.denngay, h.ghichu,h.id,h.donvi ";
            sql1 += "FROM " + user + ".hphi h INNER JOIN " + user + ".ctdaotao c ON h.mahv = c.mactdt ";
            sql = " SELECT l.mnvmay, l.nnghiep, l.td_nnghiep, l.he_dt, l.tg_dt, l.mahocham, l.mann,";
            sql += " l.tdnn, l.mann1, l.tdnn1, h.NAMQLNN, l.mann2, l.tdnn2, l.mann3,";
            sql += " l.tdnn3, l.tdth, v.tenctdt, v.dot_tt, v.sohd, v.hocphi,";
            sql += " to_char(v.ngayhp,'dd/mm/yyyy') AS ngayhp, v.mahv, l.mahv AS mahvll,";
            sql += " v.manv, v.thang, to_char(v.tungay,'dd/mm/yyyy') AS tungay, ";
            sql += " to_char(v.denngay,'dd/mm/yyyy') AS ngayhethan, v.ghichu,v.id,v.donvi";
            sql += " FROM (" + user + ".llnv l INNER JOIN " + user + ".hopdong h ON l.mnvmay=h.MANVMAY) ";            
            sql += " INNER JOIN (" + sql1 + ") v ON l.manv=v.manv";
            sql += " inner join " + user + ".max_lan ml on h.manv=ml.manv";
            sql += " where l.manv='" + ma_nv + "' and h.lan=ml.lan";

            return objDAL.getDataByCondition(sql);
        }
        public bool upd_lylichnvCS(string ma_nv, string ss_ld, string nc_ld, string t_ld,
            string noic_ld, string sdkld_ld, string ndksdld_ld, string nc_bhxh, string noic_bhxh)
        {
            return objDAL.upd_lylichnvCS(ma_nv, ss_ld, nc_ld,t_ld,noic_ld,sdkld_ld, ndksdld_ld, nc_bhxh,  noic_bhxh, user);
        }
        public bool upd_hopdongCS(string ma_nv, string s_bhxh,
            int stktbhxh_bhxh, string ctcq_ld, string nchuyen_ld, decimal tgbhxh_bhxh)
        {
            return objDAL.upd_hopdongCS( ma_nv,  s_bhxh,
             stktbhxh_bhxh,  ctcq_ld,  nchuyen_ld,  tgbhxh_bhxh,user);
        }
        public bool upd_BaohiemYteCS(string ma_nv, string st_bhyt, string nc_bhyt,
            string t_bhyt, string tht_bhyt, string d_bhyt, string tbv_bhyt,
            string bhtn24_bhyt, string bhnt_bhyt, string bhk_bhyt)
        {
            return objDAL.upd_BaohiemYteCS(ma_nv, st_bhyt, nc_bhyt,
             t_bhyt, tht_bhyt, d_bhyt, tbv_bhyt,
             bhtn24_bhyt, bhnt_bhyt, bhk_bhyt, user);
        }
        public bool DelCSllnv(string ma)
        {
            sql = "UPDATE "+user+".llnv SET sold='',ngaysold=null,tinhsld='',";
            sql += " huyensld='',dkldso='',ngaydkld=null,ngaycap=null,";
            sql += " matinhbhxh='' WHERE manv='" + ma + "'";
            return Execute_data(sql);
        }
        public bool DelCShd(string ma)
        {
            sql = "UPDATE "+user+".hopdong SET sobhxh=null,k_tinhbhxh=0,";
            sql += " sld_tudau='',sld_ngaydi=null WHERE manv='" + ma + "'";
            return Execute_data(sql);
        }
        public bool upd_giacanhllnv(string ma_nv, int gdls_gc, int tthn_gc,int gdtb_gc)
        {
            return objDAL.upd_giacanhllnv(ma_nv,  gdls_gc,  tthn_gc,gdtb_gc,user);
        }
        public bool upd_giacanhhopdong(string ma_nv, string ddgc_gc)
        {
            return objDAL.upd_giacanhhopdong(ma_nv, ddgc_gc, user);
        }
        public bool upd_giacanh(decimal id, string ma_nv, string ho_ten, string quan_he,
            string phai_, string nam_sinh, string que_quan,string nghe_nghiep,string ghichu,string ten_nd)
        {
            return objDAL.upd_giacanh(id, ma_nv, ho_ten, quan_he,
                 phai_, nam_sinh, que_quan, nghe_nghiep,ghichu,ten_nd,user);
        }
        public DataSet load_ttgiacanh(string m_manv)
        {
            sql="SELECT l.mnvmay,l.manv, l.hoan_canh, l.lietsi, h.dacdiem,";
            sql+=" g.htendtg, g.madt, d.tendt, g.phai, g.namsinh, g.quequan,g.nghenghiep ";
            sql += " FROM (" + user + ".llnv l  INNER JOIN " + user + ".hopdong h ON h.manv=l.manv) ";
            sql += " INNER JOIN (" + user + ".giacanh g INNER JOIN " + user + ".doituong d ON g.madt=d.madt) ";
            sql += " ON h.manv=g.manv where l.mnvmay='" + m_manv + "' ORDER BY g.madt ";
            return objDAL.getDataByCondition(sql);
        }
        public string Caps(string s)
        {
            string ret = null;
            StringBuilder sb = new StringBuilder(s);
            sb[0] = Char.ToUpper(sb[0]);
            int num = 0; int ispace = 0;
            while (num < sb.Length)
            {
                if (Char.IsWhiteSpace(sb[num]))
                {
                    ispace++;
                }

                if (!Char.IsWhiteSpace(sb[num]))
                {
                    if (ispace > 0 && num > 0)
                    {
                        sb[num] = Char.ToUpper(sb[num]);
                        ispace = 0;
                    }
                }
                num++;
            }
            num = 0;
            ispace = 0;
            while (num < sb.Length)
            {
                if (Char.IsWhiteSpace(sb[num]))
                {
                    if (ispace < 1 && num > 0)
                    {
                        ret += sb[num];
                    }
                    ispace++;
                }
                else
                {
                    ret += sb[num];
                    ispace = 0;
                }
                num++;
            }
            return ret;
        }
        public bool upd_DMHOCVAN(string ma, string ten)
        {
            return objDAL.upd_DMHOCVAN(ma, ten, user);
        }
        public bool upd_DMNGOAINGU(string ma, string ten)
        {
            return objDAL.upd_DMNGOAINGU(ma, ten, user);
        }
        public bool upd_DMCTDAOTAO(string ma, string ten,string tenvt, string manhom,decimal stt)
        {
            return objDAL.upd_DMCTDAOTAO(ma, ten, tenvt, manhom,stt, user);
        }
        public bool upd_DMBANGCAP(string ma, string ten,string mabc, int stt )
        {
            return objDAL.upd_DMBANGCAP(ma, ten, mabc, stt, user);
        }
        public bool upd_DMNGHIVIEC(string ma, string ten)
        {
            return objDAL.upd_DMNGHIVIEC(ma, ten, user);
        }
        public bool upd_nghiphep(string ma, string ten, string nhombc, decimal stt, string kihieu)
        {
            return objDAL.upd_nghiphep(ma, ten, nhombc, stt,user, kihieu);
        }
        //cap nhat dm chi bo
        public bool upd_dmchibo(string ma, string ten,string nhom ,decimal stt,string user)
        {
            return objDAL.upd_dmchibo(ma, ten,nhom,stt,user);
        }
        //cap nhat dm chuc vu dang
        public bool upd_dmchucvudang(string ma, string ten,string nhom ,decimal stt,decimal hs, string user)
        {
            return objDAL.upd_dmchucvudang(ma, ten,nhom, stt, hs, user);
            
        }
        //dm le tet
        public bool Insert_DMNGAYLE(string ngay_nl, string lydo_nl,int loai,string mmyy)
        {
            return objDAL.Insert_DMNGAYLE(ngay_nl, lydo_nl,loai,mmyy, user);
        }
        public bool Insert_dsthaisan(string manv, DateTime tungay, DateTime denngay, decimal songay, decimal tile, string ghichu)
        {
            return objDAL.Insert_dsthaisan(manv, tungay, denngay, songay, tile, ghichu,user);
        }
        public bool upd_DMDANTOC(string ma, string ten)
        {
            return objDAL.upd_DMDANTOC(ma, ten, user);
        }
        public bool upd_DMCHUCDANH(string ma, string ten,string tenvt,string nhombc, decimal sott)
        {
            return objDAL.upd_DMCHUCDANH(ma, ten,tenvt,nhombc, sott, user);
        }
        public bool upd_lylichnvDoanThe(string ma_nv, string dtct_l, string ng_l,
            string ntgcm_l, string ntl_l, string nvd_l, string ndb_l,
            string nct_l, string tpxt_l, string tpbt_l, string ntn_l,string ncdi_dt,string noicdi_dt,string noicden_dt)

        {
            return objDAL.upd_lylichnvDoanThe(ma_nv, dtct_l, ng_l, ntgcm_l, ntl_l, nvd_l, ndb_l, nct_l, tpxt_l, tpbt_l, ntn_l,ncdi_dt,noicdi_dt,noicden_dt, user);
        }
        public bool upd_hopdongDoanThe(string ma_nv, string dang_h, string std_h,
            string ncd_h, string cb_h, string cd_h, string t1_h, string st1_h, string cdp_h,
            string nkn_h, string doan_h, string ncden_h, string t2_h, string st2_h,
            string dp_h, string d1_h, string dt1_h, string cq1_h, string d2_h,
            string dt2_h, string cq2_h)
        {
            return objDAL.upd_hopdongDoanThe(ma_nv,  dang_h,  std_h,
             ncd_h,  cb_h,  cd_h,  t1_h,  st1_h,  cdp_h,
             nkn_h,  doan_h,  ncden_h,  t2_h,  st2_h,
             dp_h,  d1_h,  dt1_h,  cq1_h,  d2_h,
             dt2_h,  cq2_h,  user);
        }
        public bool upd_congviec(decimal id, string ma_nv, string tu_ngay,string tu_nam, string den_ngay,string den_nam, string don_vi, string he_so, string ma_cd, string ma_cv,string hs_cv,string hs_vk,string hs_ud,string hs_k,string ten_nd,int kt_ct)
        {
            return objDAL.upd_congviec(id, ma_nv, tu_ngay,tu_nam, den_ngay,den_nam , don_vi, he_so, ma_cd, ma_cv,hs_cv,hs_vk,hs_ud,hs_k,ten_nd,kt_ct,user);
        }
        private void upd_tbhxh(string mm, string yyyy, string mmyy)
        {
            sql = " update luong.bcc_" + mmyy + " set t_bhxh= c.t_bhxh from";
            sql += " (select l.manv";
            sql += " ,round(case h.bhxh=1 when hb.luongkhoan='true' then h.sotien_hd*mbhxh_hs else ";
            sql += " (case when hb.hs_lc='true' then h.hs_lc else 0 end + case when hb.hs_vk='true' then h.hs_lc*h.hs_vk else 0 end";
            sql += " +case when hb.hs_cv='true' then h.hs_cv else 0 end +case when hb.hs_kv='true' then h.hs_kv else 0 end";
            sql += " +case when hb.hs_tn='true' then h.hs_tn else 0 end +case when hb.hs_dh='true' then h.hs_dh else 0 end)*mbhxh_hs*mmltt end,2) as t_bhxh";
            sql += " from nhansu.hopdong h inner join nhansu.llnv l on h.manv=l.manv";
            sql += " left join luong.heso_bh hb on mmyy='" + mmyy + "'";
            sql += " join luong.heso hs on mm='" + mm + "' and yyyy='" + yyyy + "'";
            sql += " inner join nhansu.max_lan ml on h.manv=ml.manv";
            sql += " where h.lan =ml.lan and hb.loaibh='bhxh'";
            sql += " and hb.loaihd=h.loaihd) as c where luong.bcc_" + mmyy + ".manv=c.manv";
            objDAL.Upd_data(sql);
        }
        private void upd_tbhyt(string mm, string yyyy, string mmyy)
        {
            sql = " update luong.bcc_" + mmyy + " set t_bhyt= c.t_bhyt from";
            sql += " (select  l.manv";
            sql += " ,round(case h.bhyt=1 when hb.luongkhoan='true' then h.sotien_hd*mbhyt_hs else ";
            sql += " (case when hb.hs_lc='true' then h.hs_lc else 0 end + case when hb.hs_vk='true' then h.hs_lc*h.hs_vk else 0 end";
            sql += " +case when hb.hs_cv='true' then h.hs_cv else 0 end +case when hb.hs_kv='true' then h.hs_kv else 0 end";
            sql += " +case when hb.hs_tn='true' then h.hs_tn else 0 end +case when hb.hs_dh='true' then h.hs_dh else 0 end)*mbhyt_hs*mmltt end,2) as t_bhyt";
            sql += " from nhansu.hopdong h inner join nhansu.llnv l on h.manv=l.manv";
            sql += " left join luong.heso_bh hb on mmyy='" + mmyy + "'";
            sql += " join luong.heso hs on mm='" + mm + "' and yyyy='" + yyyy + "'";
            sql += " inner join nhansu.max_lan ml on h.manv=ml.manv";
            sql += " where h.lan =ml.lan and hb.loaibh='bhyt'";
            sql += " and hb.loaihd=h.loaihd) as c where luong.bcc_" + mmyy + ".manv=c.manv";
            objDAL.Upd_data(sql);
        }
        private void upd_tbhtn(string mm, string yyyy, string mmyy)
        {
            sql = " update luong.bcc_" + mmyy + " set t_bhtn= c.t_bhtn from";
            sql += " (select  l.manv";
            sql += " ,round(case h.bhtn=1 when hb.luongkhoan='true' then h.sotien_hd*mbhtn_hs else ";
            sql += " (case when hb.hs_lc='true' then h.hs_lc else 0 end + case when hb.hs_vk='true' then h.hs_lc*h.hs_vk else 0 end";
            sql += " +case when hb.hs_cv='true' then h.hs_cv else 0 end +case when hb.hs_kv='true' then h.hs_kv else 0 end";
            sql += " +case when hb.hs_tn='true' then h.hs_tn else 0 end +case when hb.hs_dh='true' then h.hs_dh else 0 end)*mbhtn_hs*mmltt end,2) as t_bhtn";
            sql += " from nhansu.hopdong h inner join nhansu.llnv l on h.manv=l.manv";
            sql += " left join luong.heso_bh hb on mmyy='" + mmyy + "'";
            sql += " join luong.heso hs on mm='" + mm + "' and yyyy='" + yyyy + "'";
            sql += " inner join nhansu.max_lan ml on h.manv=ml.manv";
            sql += " where h.lan =ml.lan and hb.loaibh='bhtn'";
            sql += " and hb.loaihd=h.loaihd) as c where luong.bcc_" + mmyy + ".manv=c.manv";
            objDAL.Upd_data(sql);
        }
        //cap nhat tien tru bao hiem
        private void upd_tkpcd(string mm, string yyyy, string mmyy)
        {
            sql = " update luong.bcc_" + mmyy + " set t_kpcd= c.t_kpcd from";
            sql += " (select  l.manv";
            sql += " ,round(case when hb.luongkhoan='true' then h.sotien_hd*mkpcd_hs else ";
            sql += " (case when hb.hs_lc='true' then h.hs_lc else 0 end + case when hb.hs_vk='true' then h.hs_lc*h.hs_vk else 0 end";
            sql += " +case when hb.hs_cv='true' then h.hs_cv else 0 end +case when hb.hs_kv='true' then h.hs_kv else 0 end";
            sql += " +case when hb.hs_tn='true' then h.hs_tn else 0 end +case when hb.hs_dh='true' then h.hs_dh else 0 end)*mkpcd_hs*mmltt end,2) as t_kpcd";
            sql += " from nhansu.hopdong h inner join nhansu.llnv l on h.manv=l.manv";
            sql += " left join luong.heso_bh hb on mmyy='" + mmyy + "'";
            sql += " join luong.heso hs on mm='" + mm + "' and yyyy='" + yyyy + "'";
            sql += " inner join nhansu.max_lan ml on h.manv=ml.manv";
            sql += " where h.lan =ml.lan and hb.loaibh='kpcd'";
            sql += " and hb.loaihd=h.loaihd) as c where luong.bcc_" + mmyy + ".manv=c.manv";
            objDAL.Upd_data(sql);
        }
        //tao du lieu he so luong nhan vien thang 
        public void taodulieu_hesonv(string s_mmyy,string mm,string yyyy)
        {
            DateTime d1, d2;
            d1 = new DateTime(int.Parse(yyyy), int.Parse(mm), 1);
            d2 = d1.AddMonths(1);
            string m_dk = "";
            m_dk = " (to_timestamp(to_char(h.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') <= to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')) ";
            m_dk += " and (h.ngaynghi is null or to_timestamp(to_char(h.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            //
            sql = "insert into " + user + ".heso_nv_"+s_mmyy+"";
            sql += "(manv,madv,mmyy,bhxh,bhyt,bhtn,kpcd,h_hsbdhv,h_hslc,h_hscv,h_hsvk,h_hstn,h_hskv,h_hsdh,h_hsdb,h_hsld,";
            sql += " h_hsth,h_hsud,h_pcta,h_pctx,h_pctdt,h_pckh,h_sotienhd,h_lhdbantg,h_stbantg,h_luongsp,h_tongluong,thuviec,dihoc,luutruhs)";

            sql += " select l.manv,l.madv,'"+s_mmyy+"',h.bhxh,h.bhyt,h.bhtn";
            sql += " ,case when (h.cdoanphi is not null and to_timestamp(to_char(h.cdoanphi,'yyyy/mm/dd'),'yyyy/mm/dd') <= to_timestamp('"+d1.ToString("yyyy/MM/dd",null)+"','yyyy/mm/dd')) then 1 else 0 end as kpcd";
            sql += " ,case when h.heso<>'' then h.heso else '0' end as heso,h.hs_lc,h.hs_cv,h.hs_vk,h.hs_tn,h.hs_kv,h.hs_dh,h.hs_db,h.hs_ld,h.hs_th,h.hs_ud,h.pc_ta,h.pc_tx,pc_tdt,h.pc_khac";
            sql += " ,case when (h.ngaygiulhd is not null and to_timestamp(to_char(h.ngaygiulhd,'yyyy/mm/dd'),'yyyy/mm/dd') <= to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/mm/dd')) then h.sotien_hd else 0 end as sotien_hd";
            sql += " ,case when h.lhd_bantg <> '' then h.lhd_bantg else '' end as lhd_bantg,h.st_bantg,h.luong_sp,h.tongluong";
            sql += " ,case when h.loaihd='TV' then 1 else 0 end as thuviec,case when h.magc ='02' then 1 else 0 end as dihoc,case when h.magc ='01' then 1 else 0 end as luutruhs ";
            sql += " from " + user+ ".hopdong h inner join " + user + ".llnv l on h.manv=l.manv ";
            sql += " inner join " + user + ".max_lan ml on h.manv=ml.manv";
            sql += " left join " + user + ".mahd mh on h.loaihd=mh.mahd";
            sql += " where h.lan =ml.lan and " + m_dk;
            objDAL.Upd_data(sql);
        }
        // tao du lieu bang tong hop cham cong
        public bool taodulieu_thchamcong(string s_mmyy)
        {
            sql = " select l.manv,l.madv ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and ngaynghi is  null ";
            sql += " order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".th_chamcong_"+s_mmyy+" (manv,madv,mmyy) values ";
                sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                try
                {
                    objDAL.Upd_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        // Ham tao du lieu cham cong
        public bool taodulieu_chamcong(string s_mmyy,string yyyy)
        {
            //bool gttv = false;
            sql = " select l.manv,l.madv  ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql+="  where hd.lan=ml.lan and ngaynghi is  null order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".chamcong_"+s_mmyy+" (manv,madv,mmyy) values ";
                sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                try
                {
                    objDAL.Upd_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            kt_ngaynghiphep(s_mmyy, yyyy);
            return true;
        }
        public void kt_ngaynghiphep(string s_mmyy, string yyyy)
        {
            if (bVantay(s_mmyy))
            {
                if (bMmyy(Mmyy_truoc(s_mmyy)))
                {
                    upd_ngayccphep_vt(mm_truoc(s_mmyy), yyyy, Mmyy_truoc(s_mmyy));
                }
            }
            else
            {
                if (bMmyy(Mmyy_truoc(s_mmyy)))
                {
                    upd_ngayccphep(mm_truoc(s_mmyy), yyyy, Mmyy_truoc(s_mmyy));
                }
            }
        }
        // cap nhat so ngay nghi phep cua nhan vien xuong bang phep nam
        private bool upd_ngayccphep(string thang, string nam, string mmyy_truoc)
        {
            DataSet ds_tmp = new DataSet();
            string sql = "";
            int n_nam = Int32.Parse(nam);
            if (thang == "1")
            {
                n_nam = n_nam - 1;
            }
            sql = "select ll.manv,sum(";
            sql += " case when n1 in ('PN') then 1 when n1 in ('P2') then 0.5 else 0 end + case when n2 in ('PN') then 1 when n2 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n3 in ('PN') then 1 when n3 in ('P2') then 0.5 else 0 end + case when n4 in ('PN') then 1 when n4 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n5 in ('PN') then 1 when n5 in ('P2') then 0.5 else 0 end + case when n6 in ('PN') then 1 when n6 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n7 in ('PN') then 1 when n7 in ('P2') then 0.5 else 0 end + case when n8 in ('PN') then 1 when n8 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n9 in ('PN') then 1 when n9 in ('P2') then 0.5 else 0 end + case when n10 in ('PN') then 1 when n10 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n11 in ('PN') then 1 when n11 in ('P2') then 0.5 else 0 end + case when n12 in ('PN') then 1 when n12 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n13 in ('PN') then 1 when n13 in ('P2') then 0.5 else 0 end + case when n14 in ('PN') then 1 when n14 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n15 in ('PN') then 1 when n15 in ('P2') then 0.5 else 0 end + case when n16 in ('PN') then 1 when n16 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n17 in ('PN') then 1 when n17 in ('P2') then 0.5 else 0 end + case when n18 in ('PN') then 1 when n18 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n19 in ('PN') then 1 when n19 in ('P2') then 0.5 else 0 end + case when n20 in ('PN') then 1 when n20 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n21 in ('PN') then 1 when n21 in ('P2') then 0.5 else 0 end + case when n22 in ('PN') then 1 when n22 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n23 in ('PN') then 1 when n23 in ('P2') then 0.5 else 0 end + case when n24 in ('PN') then 1 when n24 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n25 in ('PN') then 1 when n25 in ('P2') then 0.5 else 0 end + case when n26 in ('PN') then 1 when n26 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n27 in ('PN') then 1 when n27 in ('P2') then 0.5 else 0 end + case when n28 in ('PN') then 1 when n28 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n29 in ('PN') then 1 when n29 in ('P2') then 0.5 else 0 end + case when n30 in ('PN') then 1 when n30 in ('P2') then 0.5 else 0 end";
            sql += "+ case when n31 in ('PN') then 1 when n31 in ('P2') then 0.5 else 0 end) as ts_ngayccphep ";
            sql += " from " + user + ".chamcong_" + mmyy_truoc + " cc inner join "+user+".llnv ll on ll.manv=cc.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and cc.mmyy='" + mmyy_truoc.Trim() + "'";
            sql += " group by ll.manv";
            if(bMmyy(mmyy_truoc.Trim()))
            {
                ds_tmp = objDAL.get_data(sql);
            }
            else
            {
                ds = null;
            }
            if (ds_tmp!=null)
            {
                foreach (DataRow dr in ds_tmp.Tables[0].Rows)
                {
                    sql = " update " + user + ".phepnam_" + n_nam.ToString() + " set t" + thang + "=" + dr["ts_ngayccphep"].ToString() + " where manv='" + dr["manv"].ToString() + "' and nam='" + n_nam.ToString() + "'";
                    try
                    {
                        objDAL.Upd_data(sql);                           
                    }
                    catch { }
                }
            }
            return true;
        }
        // cap nhat so ngay nghi phep cua nhan vien xuong bang phep nam khi cham cong van tay
        private bool upd_ngayccphep_vt(string thang, string nam, string mmyy_truoc)
        {
            DataSet ds_nv = new DataSet();
            DataSet ds_cc = new DataSet();
            string sql = "";
            int n_nam = Int32.Parse(nam);
            if (thang == "1")
            {
                n_nam = n_nam - 1;
            }
            ds_nv = get_dsnvchamcong_thang(mmyy_truoc);
            ds_cc = get_dsnvchamcongvt_thang(mmyy_truoc);
            if (ds_cc !=null)
            {
                decimal ts_phep = 0;
                foreach (DataRow dr in ds_nv.Tables[0].Rows)
                {
                    ts_phep = 0;
                    DataRow[] dr1 = ds_cc.Tables[0].Select("manv='" + dr["manv"].ToString().Trim() + "'", "manv asc");
                    foreach (DataRow r in dr1)
                    {
                        if (r["lydo"].ToString().Trim() == "P2")
                        {
                            ts_phep += decimal.Parse("0.5");
                        }
                        if (r["lydo"].ToString().Trim() == "PN")
                        {
                            ts_phep += 1;
                        }
                    }
                    dr["ts_phep"] = ts_phep.ToString();
                    sql = " update " + user + ".phepnam_" + n_nam.ToString() + " set t" + thang + "=" + dr["ts_phep"].ToString() + " where manv='" + dr["manv"].ToString() + "' and nam='" + n_nam.ToString() + "'";
                    try
                    {
                        objDAL.Upd_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }                
            return true;
        }
        public DataSet get_dsnvchamcong_thang(string s_mmyy)
        {
            sql = " select l.manv,0.0 as ts_phep,0.0 as ts_ngaytruc,0.0 as ts_bu,0.0 as ts_ngayton";
            sql += " from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".th_chamcong_"+s_mmyy+" tc on l.manv=tc.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null";
            return objDAL.get_data(sql);
        }
        public DataSet get_dsnvchamcongvt_thang(string s_mmyy)
        {
            sql = " select l.manv,cn.lydo";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".ccnghi_" + s_mmyy + " cn on l.manv=cn.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null";
            return objDAL.get_data(sql);
        }
        
        public bool taodulieu_chamcong_thuongtruc(string s_mmyy,string yyyy)
        {
            //bool gttv = false;
            sql = " select l.manv,l.madv  ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += "  where hd.lan=ml.lan and ngaynghi is  null order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".chamcong_thuongtruc_"+s_mmyy+" (manv,madv,mmyy) values ";
                sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                try
                {
                    objDAL.Upd_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            kt_ngaynghibu(s_mmyy,yyyy);
            return true;
        }
        public void kt_ngaynghibu(string s_mmyy,string nam)
        {
            int n_nam = Int32.Parse(nam);
            string thang = mm_truoc(s_mmyy).PadLeft(2,'0');
            if (thang == "01")
            {
                n_nam = n_nam - 1;
            }
            string s_ncuoithang = get_ncuoithangddmmyyyy(thang.PadLeft(2, '0'), n_nam.ToString());
            if (bVantay(s_mmyy))
            {
                if (bMmyy(Mmyy_truoc(s_mmyy)))
                {
                    upd_ngayccnghibu_vt(s_mmyy, Mmyy_truoc(s_mmyy), s_ncuoithang);                    
                }
            }
            else
            {
                if (bMmyy(Mmyy_truoc(s_mmyy)))
                {
                    upd_ngaytruccon(s_mmyy, Mmyy_truoc(s_mmyy));
                }
            }
        }
        private void upd_ngaytruccon(string mmyy, string mmyy_truoc)
        {
            string sql = "";
            sql = " update " + user + ".chamcong_thuongtruc_" + mmyy.Trim() + " set ts_ngayton =c.ts_ngaytruccon from (select l.manv,cc.mmyy,sum( ";
            sql += "case when cc_tt.n1 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n2 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n3 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n4 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n5 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n6 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n7 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n8 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n9 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n10 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n11 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n12 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n13 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n14 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n15 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n16 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n17 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n18 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n19 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n20 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n21 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n22 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n23 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n24 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n25 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n26 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n27 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n28 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n29 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end + case when cc_tt.n30 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end";
            sql += "+ case when cc_tt.n31 in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end )";
            sql += "- sum(";
            sql += " case when cc.n1 in ('NB') then 1 when cc.n1 in ('B2') then 0.5 else 0 end + case when cc.n2 in ('NB') then 1 when cc.n2 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n3 in ('NB') then 1 when cc.n3 in ('B2') then 0.5 else 0 end + case when cc.n4 in ('NB') then 1 when cc.n4 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n5 in ('NB') then 1 when cc.n5 in ('B2') then 0.5 else 0 end + case when cc.n6 in ('NB') then 1 when cc.n6 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n7 in ('NB') then 1 when cc.n7 in ('B2') then 0.5 else 0 end + case when cc.n8 in ('NB') then 1 when cc.n8 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n9 in ('NB') then 1 when cc.n9 in ('B2') then 0.5 else 0 end + case when cc.n10 in ('NB') then 1 when cc.n10 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n11 in ('NB') then 1 when cc.n11 in ('B2') then 0.5 else 0 end + case when cc.n12 in ('NB') then 1 when cc.n12 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n13 in ('NB') then 1 when cc.n13 in ('B2') then 0.5 else 0 end + case when cc.n14 in ('NB') then 1 when cc.n14 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n15 in ('NB') then 1 when cc.n15 in ('B2') then 0.5 else 0 end + case when cc.n16 in ('NB') then 1 when cc.n16 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n17 in ('NB') then 1 when cc.n17 in ('B2') then 0.5 else 0 end + case when cc.n18 in ('NB') then 1 when cc.n18 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n19 in ('NB') then 1 when cc.n19 in ('B2') then 0.5 else 0 end + case when cc.n20 in ('NB') then 1 when cc.n20 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n21 in ('NB') then 1 when cc.n21 in ('B2') then 0.5 else 0 end + case when cc.n22 in ('NB') then 1 when cc.n22 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n23 in ('NB') then 1 when cc.n23 in ('B2') then 0.5 else 0 end + case when cc.n24 in ('NB') then 1 when cc.n24 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n25 in ('NB') then 1 when cc.n25 in ('B2') then 0.5 else 0 end + case when cc.n26 in ('NB') then 1 when cc.n26 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n27 in ('NB') then 1 when cc.n27 in ('B2') then 0.5 else 0 end + case when cc.n28 in ('NB') then 1 when cc.n28 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n29 in ('NB') then 1 when cc.n29 in ('B2') then 0.5 else 0 end + case when cc.n30 in ('NB') then 1 when cc.n30 in ('B2') then 0.5 else 0 end";
            sql += "+ case when cc.n31 in ('NB') then 1 when cc.n31 in ('B2') then 0.5 else 0 end ";
            sql += " ) as ts_ngaytruccon";
            sql += " from " + user + ".llnv l left join " + user + ".chamcong_" + mmyy_truoc + " cc on l.manv=cc.manv";
            sql += " left join " + user + ".chamcong_thuongtruc_" + mmyy_truoc + " cc_tt on l.manv=cc_tt.manv ";
            sql += " group by l.manv,cc.mmyy order by manv) as c where c.manv=chamcong_thuongtruc_" + mmyy + ".manv ";
            if (bMmyy(mmyy_truoc))
            {
                objDAL.Upd_data(sql);
            }
        }
        // cap nhat so ngay nghi phep cua nhan vien xuong bang phep nam khi cham cong van tay
        private bool upd_ngayccnghibu_vt(string s_mmyy,string mmyy_truoc,string s_ncuoithang)
        {
            DataSet ds_nv = new DataSet();
            DataSet ds_cc = new DataSet();
            DataSet ds_nb = new DataSet();
            string sql = "";
            ds_nv = get_dsnvchamcong_thang(mmyy_truoc);
            ds_cc = get_nvccthuongtruc_vt(mmyy_truoc, s_mmyy, "TR", s_ncuoithang);
            ds_nb = get_dsnvchamcongvt_thang(mmyy_truoc);
            if (ds_cc!=null && ds_nb!=null)
            {
                decimal ts_ngayton = 0,ts_ngaytruc=0,ts_ngaybu=0;
                foreach (DataRow dr in ds_nv.Tables[0].Rows)
                {
                    ts_ngayton = 0; ts_ngaybu = 0; ts_ngaytruc = 0;
                    DataRow[] dr1 = ds_nb.Tables[0].Select("manv='" + dr["manv"].ToString().Trim() + "'", "manv asc");
                    DataRow[] dr2 = ds_cc.Tables[0].Select("manv='" + dr["manv"].ToString().Trim() + "'", "manv asc");
                    foreach (DataRow r in dr1)
                    {
                        if (r["lydo"].ToString().Trim() == "B2")
                        {
                            ts_ngaybu += decimal.Parse("0.5");
                        }
                        if (r["lydo"].ToString().Trim() == "NB")
                        {
                            ts_ngaybu += 1;
                        }
                    }
                    dr["ts_bu"] = ts_ngaybu.ToString();
                    foreach (DataRow r in dr2)
                    {
                        ts_ngaytruc += 1;
                    }
                    dr["ts_ngaytruc"] = ts_ngaytruc.ToString().Trim();
                    ts_ngayton = ts_ngaytruc - ts_ngaybu;
                    dr["ts_ngayton"] = ts_ngayton.ToString();

                    sql = " update " + user + ".th_chamcong_" + s_mmyy + " set sn_tructon=" + ts_ngayton + " where manv='" + dr["manv"].ToString() + "' and mmyy='" + mmyy_truoc + "'";
                    try
                    {
                        objDAL.Upd_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        public DataSet get_nvccthuongtruc_vt(string s_mmyy, string s_mmyy1, string nhomca, string s_ncuoithang)
        {
            sql = " select cv.manv,cv.lydo as lydo";
            sql += " from " + user + ".ccvao_" + s_mmyy + " cv inner join " + user + ".ccra_" + s_mmyy + " cr on cv.manv=cr.manv";
            sql += " inner join "+user+".lichlv lv on cv.maca=lv.maca ";
            sql += " where cv.maca=cr.maca and cv.kt_ng=cr.kt_ng";
            sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
            if (nhomca.Trim() != "")
                sql += " and lv.manhom ='" + nhomca.Trim() + "'";
            if (bMmyy(s_mmyy1))
            {
                sql += " union";
                sql += " select cv.manv,cv.lydo as lydo";
                sql += " from " + user + ".ccvao_" + s_mmyy + " cv inner join " + user + ".ccra_" + s_mmyy1 + " cr on cv.manv=cr.manv";
                sql += " inner join " + user + ".lichlv lv on cv.maca=lv.maca ";
                sql += " where cv.maca=cr.maca and cv.kt_ng=cr.kt_ng";
                sql += " and to_timestamp(to_char(cv.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + s_ncuoithang.Trim() + "','dd/MM/yyyy')";
                sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
                if (nhomca.Trim() != "")
                    sql += " and lv.manhom = '" + nhomca.Trim() + "'";
            }
            return objDAL.get_data(sql);
        } 
        public bool taodulieu_chamcong_ng(string s_mmyy)
        {
            sql = " select a.manv from " + user + ".ngoaigio_"+ Mmyy_truoc(s_mmyy)+" a ";
            DataSet ds = new DataSet();
            if (bMmyy(Mmyy_truoc(s_mmyy)))
            {
                ds = objDAL.getDataByCondition(sql);
            }
            else
            {
                ds = null;
            }
            if (ds!=null)
            {
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    sql = "insert into " + user + ".ngoaigio_" + s_mmyy + " (manv,mmyy) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + s_mmyy + "')";
                    try
                    {
                        objDAL.Upd_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        //tao danh sach lam thuon quy
        public bool taodulieu_chamcong_thuongquy(string s_mmyy)
        {
            try
            {
                sql = " select a.manv,a.hoten from " + user + ".thuongquy_" + Mmyy_truoc(s_mmyy) + " a ";
                DataSet ds = new DataSet();
                if (bMmyy(Mmyy_truoc(s_mmyy)))
                {
                    ds = objDAL.getDataByCondition(sql);
                }
                else
                {
                    ds = null;
                }
                if (ds!=null)
                {
                    foreach (DataRow r in ds.Tables[0].Rows)
                    {
                        sql = "insert into " + user + ".thuongquy_" + s_mmyy + " (manv,mmyy,hoten) values ";
                        sql += " ('" + r["manv"].ToString() + "','" + s_mmyy + "','" + r["hoten"].ToString() + "')";
                        objDAL.Upd_data(sql);
                    }
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool taodanhsach_dutrulamngoaigio(string s_mmyy)
        {
            sql = " select l.manv,l.ho||' '||l.ten as hoten  ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv ";
            sql += "  where hd.lan=ml.lan and ngaynghi is  null order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".ngoaigio_"+s_mmyy+" (manv,mmyy,hoten) values ";
                sql += " ('" + r["manv"].ToString() + "','" + s_mmyy + "','" + r["hoten"].ToString() + "')";
                try
                {
                    objDAL.Upd_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public bool taodulieu_chamcong_tructc(string s_mmyy)
        {
            //bool gttv = false;
            sql = " select l.manv,l.madv  ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += "  where hd.lan=ml.lan and ngaynghi is  null order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".chamcong_tructc_"+s_mmyy+" (manv,madv,mmyy) values ";
                sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                try
                {
                    objDAL.Upd_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        public bool taodulieu_phepnam(string s_yyyy,string s_mmyy)
        {            
            string sql = "",sql1="", s_phepthem = "";
            string d1 = s_yyyy + "/01/01";
            string d2 = s_yyyy + "02/01";
            s_phepthem = phepthem(s_mmyy).Trim();
            sql = " select l.manv,l.madv,hd.sonphep,hd.lan ";
            if (s_phepthem.Substring(0, 1).Trim() == "1")
                sql += ",floor(extract(year from age(to_timestamp('" + d1 + "','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd')))/5) as phepthem";
            if (s_phepthem.Substring(1, 1).Trim() == "1")
            {
                sql += ",case when extract(year from age(to_timestamp('" + d1 + "','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd'))) between 5 and 10 then 1 else";
                sql += " floor(extract(year from age(to_timestamp('" + d1+ "','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd')))/5) end as phepthem";
            }
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2 + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1+ "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            DataSet ds = new DataSet();
            ds = objDAL.getDataByCondition(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                sql = "insert into " + user + ".phepnam_"+s_yyyy.Trim()+" (manv,madv,nam,phep,phepthem) values ";
                sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_yyyy + "',";
                sql += " " + r["sonphep"].ToString() + "," + r["phepthem"].ToString() + ")";
                //cap nhat ngay phep them hang nam cho bang hop dong
                sql1 = " update " + user + ".hopdong set phepthem=" + r["phepthem"].ToString() + " ";
                sql1 += " where manv='" + r["manv"].ToString() + "' and lan=" + r["lan"].ToString() + "";
                try
                {
                    objDAL.Upd_data(sql);
                    objDAL.Upd_data(sql1);
                }
                catch
                {
                    return false;
                }              
            }
            return true;
        }
        
        public bool upd_hopdong(string ma_nv, string shd, string nnl, string lhd, string hlt,
            string dn, int tg, decimal mltt, string ndct, string nnv, string nct, string cct,
            decimal snp, string ntBHXH, string nhl, decimal mbhyt, int dbhxh, decimal cv, decimal tn, decimal dh, decimal ld, decimal pcdb, decimal pckv, decimal l,
            decimal th, decimal mlcb, string ncc, string dg, decimal dt, string nvk, decimal vk,
            string td, string gc, string nctnn, string ntd, int b, string loaingach, int v_nhuu, int v_bkhen, int v_tnl,
            decimal phepthem, string ngaybnngach, string ngay_tapsu, string ngay_chinhthuc,
            decimal luong_tapsu, int tg_tapsu, int nvu, string nam, int thaiSan, string ghiChuThem,
            string loaiHuongphep, string ngayPhepnam, string thuviecTN, string thuviecDN, int m_lan, string hs_bdhv,decimal congtacphi,string ghichuct)
        {
            return objDAL.upd_hopdong(ma_nv, shd,  nnl,  lhd,  hlt,
             dn,  tg,  mltt,  ndct,  nnv,  nct,  cct,
             snp,  ntBHXH,  nhl,  mbhyt,  dbhxh,  cv,  tn,  dh,  ld,  pcdb,  pckv,  l,
             th,  mlcb,  ncc,  dg,  dt,  nvk,  vk,
             td,  gc,  nctnn,  ntd,  b,  loaingach,  v_nhuu,  v_bkhen,  v_tnl,
             phepthem,  ngaybnngach,  ngay_tapsu,  ngay_chinhthuc,
             luong_tapsu,  tg_tapsu,  nvu,  nam,  thaiSan,  ghiChuThem,
             loaiHuongphep,  ngayPhepnam,  thuviecTN,  thuviecDN,m_lan,hs_bdhv,congtacphi,ghichuct, user);
        }
        public decimal next_hslc(string ma, decimal hslc)
        {
            DataTable dt = get_data("select * from "+user+".mangach").Tables[0];
            int r = 0, c = 2;
            bool bFound = false;
            for (r = 0; r < dt.Rows.Count; r++)
            {
                if (dt.Rows[r]["ma"].ToString() == ma)
                {
                    for (c = 2; c < 15; c++)//dt.Columns.Count-1
                        if (decimal.Parse(dt.Rows[r][c].ToString()) == hslc)
                        {
                            bFound = true;
                            c = c + 1;
                            if (c == 14)
                                return hslc;
                            break;
                        }
                    break;
                }
            }
            return (bFound) ? decimal.Parse(dt.Rows[r][c].ToString()) : 0;
        }
        public DataSet f_countNV()
        {
            sql = "select count(l.manv) as sonv  ";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan ";
            return objDAL.getDataByCondition(sql);
       }

        public DataSet f_sumhopdong()
        {
            sql = "select count(*) as sonv from ((((" + user + ".llnv a inner join " + user + ".hopdong c on a.manv=c.manv)";
            sql += " inner join " + user + ".bophan b on a.madv=b.madv)";
            sql += " left join " + user + ".dmcv d on c.macv=d.macv)";
            sql += " left join " + user + ".dmlydo e on c.lydonghi=e.ma)";
            sql += " where (e.thoiviec<>1 or e.thoiviec is null)";
            return objDAL.getDataByCondition(sql);
        }

        public bool upd_hopdong(string n_manv, string n_madv, string n_macv, string n_hdldso, string n_ngayhd, string n_loaihd, string n_tungay,
           string n_denngay, int n_thoigian, decimal n_luonghd, string n_ngaydenct, string n_ngaynghi, string n_ngungct, string n_chuyenct,
           int n_sonphep, string n_ngaybhxh, string n_s_begin, int n_bhyt, int n_bhxh,int n_bhtn, decimal n_hs_cv, decimal n_hs_tn, decimal n_hs_dh,
           decimal n_hs_ld, decimal n_hs_db, decimal n_hs_kv, decimal n_hs_lc,
           decimal n_hs_th, decimal n_luongct, string n_mangach, string n_lydonghi, decimal n_hs_dt, decimal n_hs_dt1, decimal n_hs_ud, string n_ngayvk, decimal n_hs_vk,
           string n_matd, string n_magc, string n_ngayctnn, string n_ngaytd, int n_bac, int n_tbnghihuu, int n_bangkhen, int n_thangnangluong,
           int n_phepthem, string n_ngaybnngach, string n_ngay_tapsu, string n_chinhthuc_lv,
           decimal n_luong_tapsu, int n_tg_tapsu, int n_ko_hoanthanh, string n_nam, int n_thaisan, string n_ghichuphep,
           string n_loaihuongphep, string n_ngayphepnam, string n_tvtungay, string n_tvdenngay, int n_lan, string n_heso, decimal n_congtacphi, decimal n_sotienhd
           ,string n_ngaygiulhd, string ghichuct, decimal thuongkhac, string n_lhdbantg, decimal n_stbantg, int n_luongsp, decimal n_pctdt, decimal n_tongluong, string ten_nd)
        {
            return objDAL.upd_hopdong_nhanvien(n_manv, n_madv, n_macv, n_hdldso, n_ngayhd, n_loaihd, n_tungay,
             n_denngay, n_thoigian, n_luonghd, n_ngaydenct, n_ngaynghi, n_ngungct, n_chuyenct,
             n_sonphep, n_ngaybhxh, n_s_begin, n_bhyt, n_bhxh,n_bhtn, n_hs_cv, n_hs_tn, n_hs_dh, n_hs_ld, n_hs_db, n_hs_kv, n_hs_lc,
             n_hs_th, n_luongct, n_mangach, n_lydonghi, n_hs_dt,n_hs_dt1,n_hs_ud, n_ngayvk, n_hs_vk,
             n_matd, n_magc, n_ngayctnn, n_ngaytd, n_bac, n_tbnghihuu, n_bangkhen, n_thangnangluong,
             n_phepthem, n_ngaybnngach, n_ngay_tapsu, n_chinhthuc_lv,
             n_luong_tapsu, n_tg_tapsu, n_ko_hoanthanh, n_nam, n_thaisan, n_ghichuphep,
             n_loaihuongphep, n_ngayphepnam, n_tvtungay, n_tvdenngay, n_lan, n_heso,n_congtacphi,n_sotienhd
             ,n_ngaygiulhd, ghichuct,thuongkhac,n_lhdbantg,n_stbantg,n_luongsp,n_pctdt,n_tongluong,ten_nd,user);
        }
        public string encode(string s)
        {
            string s1 = "", c1 = "";
            s = s.Trim();
            int c = 0;
            for (int i = 1; i <= Microsoft.VisualBasic.Strings.Len(s); i++)
            {
                c1 = Microsoft.VisualBasic.Strings.Mid(s, i, 1);
                c = Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(s, i, 1));
                if (c % 2 == 0)
                    s1 = s1 + Microsoft.VisualBasic.Strings.Chr(c + 128);
                else
                    s1 = s1 + Microsoft.VisualBasic.Strings.Chr(c + 96);
            }
            return s1;
        }
        public string decode(string s)
        {
            string s1 = "", c1 = "";
            s = s.Trim();
            int c = 0;
            for (int i = 1; i <= Microsoft.VisualBasic.Strings.Len(s); i++)
            {
                c1 = Microsoft.VisualBasic.Strings.Mid(s, i, 1);
                c = Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(s, i, 1));
                if (c % 2 == 0)
                    s1 = s1 + Microsoft.VisualBasic.Strings.Chr(c - 128);
                else
                    s1 = s1 + Microsoft.VisualBasic.Strings.Chr(c - 96);
            }
            return s1;
        }
        public bool upd_Nuocngoai(string ma_nv, int nam_, string quoc_gia,
            int thoi_gian, string dvtg, string t_ngay, string d_ngay, string cong_viec,string ten_nd,decimal id)
        {
            return objDAL.upd_Nuocngoai( ma_nv,  nam_,  quoc_gia,
             thoi_gian,  dvtg,  t_ngay,  d_ngay,  cong_viec,ten_nd, user,id);
        }
        public DataSet f_dinuocngoai(string ma_nv)
        {

            sql = "SELECT a.manv, a.nam, a.maqgia, b.tenqg, a.tgian as t_gian,";
            sql+="  a.dvtgian,to_char(a.tungay,'dd/mm/yyyy') as tu_ngay,to_char(a.denngay,'dd/mm/yyyy') as den_ngay,a.lamgi,a.id as id ";
            sql+="  FROM " + user + ".xuatcanh a INNER JOIN " + user + ".quocgia b ON a.maqgia=b.maqg ";
            sql+= " WHERE a.manv='" + ma_nv.Trim() + "' order by nam desc";

            return objDAL.getDataByCondition(sql);
        }
        public bool upd_SucKhoe(string ma_nv, string nd)
        {
            return objDAL.upd_SucKhoe(ma_nv, nd,user);
        }
        public bool upd_SucKhoe(decimal id, string ma_nv, string th_suckhoe,
            string loai_, string tu_ngay, string den_ngay, bool thuongtat, string loai_tt, string ghi_chu, string ten_nd)
        {
            return objDAL.upd_SucKhoe(id, ma_nv, th_suckhoe,
            loai_, tu_ngay, den_ngay, thuongtat, loai_tt, ghi_chu, ten_nd, user);
        }
        public DataSet f_suckhoe(string ma_nv)
        {
            sql="SELECT id,manv," ;
            sql+=" to_char(tgdau,'dd/mm/yyyy') as tg_dau," ;
            sql+=" to_char(tgcuoi,'dd/mm/yyyy') as tg_cuoi,benhly,thuongtat,ghichu,loai,loai_tt" ;
            sql+=" FROM "+user+".suckhoe where manv='" + ma_nv + "' order by tgdau desc";
            return objDAL.getDataByCondition(sql);
        }
        public string ngayhienhanh_server
        {
            get
            {
                return objDAL.ngayhienhanh_server;
            }
        }
        public void modify_tables_hm()
        {
            try
            {
                Execute_data("CREATE TABLE " + user + ".hm_treemenu(id numeric(5), id_cha numeric(5), stt numeric(5), ten text, sql text, tenfield text, captionfield text, width text, report text,readonly numeric(1) default 0, CONSTRAINT pk_hm_treemenu PRIMARY KEY (id)) WITH OIDS");
                Execute_data("ALTER TABLE " + user + ".hm_treemenu OWNER TO " + database + "");
            }
            catch { };
        }
        public DataSet get_treemenu()
        {
            string asql = "select * from "+user+".hm_treemenu";
            return get_data(asql);
        }
        public DataSet get_data_bc(DateTime v_tn, DateTime v_dn, string v_sql)
        {
            try
            {
                string asql = "", ammyy = "";
                DataSet ads = null, ads1 = null;
                if (v_tn > v_dn)
                {
                    v_tn = v_dn;
                }
                
                while (v_dn >= v_tn)
                {
                    ammyy = v_dn.Month.ToString().PadLeft(2, '0') + v_dn.Year.ToString().Substring(2);
                    asql = v_sql.Replace("medibvmmyy.", database + ammyy + ".");
                    asql = asql.Replace("medibv.", database + ".");
                    ads1 = get_data(asql);
                    if (ads1 != null)
                    {
                        if (ads == null)
                        {
                            ads = ads1;
                        }
                        else
                        {
                            ads.Merge(ads1);
                        }
                    }
                    v_dn = v_dn.AddMonths(-1);
                }
                
                return ads;
            }
            catch
            {
                return null;
            }
        }

        public DataSet get_data_bc(DateTime v_tn, DateTime v_dn, string v_sql, int v_limit)
        {
            try
            {
                if (v_tn > v_dn)
                {
                    v_tn = v_dn;
                }

                string asql = "", ammyy = "", alimit = "";
                if (v_limit > 0)
                {
                    alimit = " limit " + v_limit.ToString();
                }
                DataSet ads = null, ads1 = null;
                if (v_sql.IndexOf("medibvmmyy.") >= 0)
                {
                    while (v_dn >= v_tn)
                    {
                        ammyy = v_dn.Month.ToString().PadLeft(2, '0') + v_dn.Year.ToString().Substring(2);
                        asql = v_sql.Replace("medibvmmyy.", database + ammyy + ".");
                        asql = asql.Replace("medibv.", database + ".");
                        if (asql.IndexOf("limit") < 0 && alimit != "")
                        {
                            asql += alimit;
                        }
                        ads1 = get_data(asql);
                        if (ads1 != null)
                        {
                            if (ads == null)
                            {
                                ads = ads1;
                            }
                            else
                            {
                                ads.Merge(ads1);
                            }
                            if (ads != null)
                            {
                                if (alimit != "" && ads.Tables[0].Rows.Count >= v_limit)
                                {
                                    return ads;
                                }
                            }
                        }
                        v_dn = v_dn.AddMonths(-1);
                    }
                }
                else
                {
                    asql = v_sql.Replace("medibv.", database + ".");
                    if (asql.IndexOf("limit") < 0 && alimit != "")
                    {
                        asql += alimit;
                    }
                    ads = get_data(asql);
                }
                return ads;
            }
            catch
            {
                return null;
            }
        }
        public bool upd_treemenu(decimal v_id, decimal v_id_cha, decimal v_stt, string v_ten, string v_sql, string v_tenfield, string v_captionfield, string v_width, string v_report, decimal v_readonly)
        {
            return objDAL.upd_treemenu(v_id,  v_id_cha,  v_stt,  v_ten,  v_sql,  v_tenfield,  v_captionfield,  v_width,  v_report,  v_readonly,user);
        }
        public string get_id_hm_treemenu()
        {
            try
            {
                return get_data("select case when max(id) is null then 0 else max(id) end + 1 as stt from "+user+".hm_treemenu").Tables[0].Rows[0][0].ToString();
            }
            catch
            {
                return "1";
            }
        }
        public string get_stt_hm_treemenu(string v_id_cha)
        {
            try
            {
                if (v_id_cha == "") v_id_cha = "0";
                return get_data("select case when max(stt) is null then 0 else max(stt) end + 1 as stt from " + user + ".hm_treemenu where id_cha=" + v_id_cha + "").Tables[0].Rows[0][0].ToString();
            }
            catch
            {
                return "1";
            }
        }
        public bool del_hm_treemenu(string v_id)
        {
            try
            {
                Execute_data("delete " + user + ".hm_treemenu where id=" + v_id + " and readonly=0");
                return !(get_data("select id from " + user + ".hm_treemenu where id=" + v_id).Tables[0].Rows.Count > 0);
            }
            catch
            {
                return false;
            }
        }
        public DataSet TimNV(string ma_nv, string ho_nv, string ten_nv, string bo_phan)
        {
            string sql = "";
            sql = "SELECT l.mnvmay, l.manv, l.madv,";
            sql += " trim(l.ho) as honv,trim(l.ten) as tennv, ";
            sql += " trim(l.ho)||' '|| trim(l.ten) as ho, dmcv.diengiai, ";
            sql += " b.tenbp FROM " + user + ".llnv l inner join ";
            sql += " " + user + ".hopdong h on h.manv=l.manv";
            sql += " INNER JOIN " + user + ".bophan b ON b.madv=l.madv ";
            sql += " left join " + user + ".dmcv ON dmcv.macv = h.MACV";
            sql += " inner join "+user+".max_lan ml on h.manv=ml.manv";
            sql += " WHERE h.ngaynghi is null and h.lan=ml.lan ";
            if (ma_nv != "" && ma_nv != "00000")
                sql += " and l.manv='" + ma_nv + "'";
            if (ho_nv != "")
                sql += " and trim(upper(l.ho)) like '%" + ho_nv + "%'";
            if (ten_nv != "")
                sql += " and trim(upper(l.ten)) like '%" + ten_nv + "%'";
            if (bo_phan != "")
                sql += " and b.madv = '" + bo_phan + "'";
            sql += " order by l.manv";
            return objDAL.getDataByCondition(sql);
        }
        public DataSet f_TimNNDT(string ma_nv, string ho_nv, string ten_nv, string bo_phan)
        {

            sql = "SELECT l.mnvmay, l.manv,";
            sql += " b.madv, b.tenbp, l.ho, l.ten,";
            sql += " to_char(c.tungay,'dd/mm/yyyy') as t_ngay,";
            sql += " to_char(c.denngay,'dd/mm/yyyy') as d_ngay,";
            sql += " c.mahv, d.tenctdt as ten_ctdt, c.mabangcap,";
            sql += " e.tenbang as ten_bang, c.hehocvan,";
            sql += " f.tenhe as ten_hoc, c.ghichu as ghi_chu";
            sql += " FROM " + user + ".llnv l INNER JOIN " + user + ".bophan b ON l.madv = b.madv";
            sql += " INNER JOIN " + user + ".qtdaotao c ON l.manv = c.manv";
            sql += " INNER JOIN " + user + ".ctdaotao d ON c.mahv = d.mactdt";
            sql += " INNER JOIN " + user + ".bangcap e ON c.mabangcap = e.mabangcap";
            sql += " INNER JOIN " + user + ".hedaotao f ON c.hehocvan = f.mahe ";
            sql += " inner join " + user + ".hopdong on l.manv=hopdong.manv";
            sql += " WHERE hopdong.ngaynghi is null";
            if (ma_nv!="")
               sql+=" and l.manv='" + ma_nv.Trim() + "'";
            if (ho_nv!="")
                sql += " and trim(upper(l.ho)) like '%" + ho_nv.Trim() + "%'";
            if (ten_nv!="")
                sql += " and trim(upper(l.ten)) like '%" + ten_nv.Trim() + "%'";
            if (bo_phan != "")
                sql += " and b.madv = '" + bo_phan + "'";
            return objDAL.getDataByCondition(sql);
        }
        public DataSet f_TimNNDTNV(string ma_nv)
        {
            
            sql = "SELECT l.mnvmay, l.manv,";
            sql += " b.madv, b.tenbp, l.ho, l.ten,";
            sql += " to_char(c.tungay,'dd/mm/yyyy') as t_ngay,";
            sql += " to_char(c.denngay,'dd/mm/yyyy') as d_ngay,";
            sql += " c.mahv, d.tenctdt as ten_ctdt, c.mabangcap,";
            sql += " e.tenbang as ten_bang, c.hehocvan,";
            sql += " f.tenhe as ten_hoc, c.ghichu as ghi_chu";
            sql += " FROM " + user + ".llnv l INNER JOIN " + user + ".bophan b ON l.madv = b.madv";
            sql += " INNER JOIN " + user + ".qtdaotao c ON l.manv = c.manv";
            sql += " INNER JOIN " + user + ".ctdaotao d ON c.mahv = d.mactdt";
            sql += " INNER JOIN " + user + ".bangcap e ON c.mabangcap = e.mabangcap";
            sql += " INNER JOIN " + user + ".hedaotao f ON c.hehocvan = f.mahe ";
            sql += " inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " WHERE hd.lan=ml.lan and hd.ngaynghi is null";
            if (ma_nv != "")
                sql += " and l.manv='" + ma_nv.Trim() + "'";
            
            return objDAL.getDataByCondition(sql);

            
        }
        public bool reorder_treemenu(string v_id, int v_step)
        {
            try
            {
                DataRow r = get_data("select * from "+user+".hm_treemenu where id=" + v_id + "").Tables[0].Rows[0];
                DataRow r1;
                if (v_step > 0)
                {
                    r1 = get_data("select * from " + user + ".hm_treemenu where id_cha=" + r["id_cha"].ToString() + " and stt>" + r["stt"].ToString() + " order by stt asc").Tables[0].Rows[0];
                }
                else
                {
                    r1 = get_data("select * from " + user + ".hm_treemenu where id_cha=" + r["id_cha"].ToString() + " and stt<" + r["stt"].ToString() + " order by stt desc").Tables[0].Rows[0];
                }
                Execute_data("update " + user + ".hm_treemenu set stt=" + r1["stt"].ToString() + " where id=" + r["id"].ToString());
                Execute_data("update " + user + ".hm_treemenu set stt=" + r["stt"].ToString() + " where id=" + r1["id"].ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool upd_dmnghiviec(string n_ma, string n_ten, int n_thoiviec)
        {
            return objDAL.upd_dmnghiviec(n_ma, n_ten, n_thoiviec, user);
        }
     
        public bool Capnhat_calamviec(int maca, string tenca, string batdau,
            string ketthuc, int som, int tre, string batdauvao, string ketthucvao,
            string batdaura, string kethucra, int phaivao, int phaira, decimal sogio, string mau,string manhom)
        {
            return objDAL.Capnhat_calamviec(maca, tenca, batdau,
             ketthuc, som, tre, batdauvao, ketthucvao,
             batdaura, kethucra, phaivao, phaira, sogio, mau,manhom, user);
        }
        public bool upd_dmnganhang(int n_ma, string n_ten)
        {
            return objDAL.upd_dmnganhang(n_ma, n_ten, user);
        }

        public bool update_bacluong(string manv, string mmyy, string ngaygiubac, string bacluongcu, string ngaynangbac, string bacluongmoi,string mangachcu,string mangachmoi,string ngay,string madv,string macv)
        {
            if (objDAL.update_bacluong(manv, mmyy, ngaygiubac, bacluongcu, ngaynangbac, bacluongmoi,mangachcu,mangachmoi,ngay,madv,macv, user) > 0)
                return true;
            return false;
        }
        public bool update_vuotkhung(string manv, string mmyy, string loaiphucap, decimal hs_cu, string ngayquydinh, decimal hs_moi, string ngaygiu, string ngay,string madv,string macv)
        {

            if (objDAL.update_vuotkhung(manv, mmyy, loaiphucap, hs_cu, ngayquydinh, hs_moi, ngaygiu, ngay,madv,macv, user) > 0)
                return true;
            return false;
        }
        public bool update_kyluat(string manv,string yyyy, string nam, string ghichu)
        {
            if (objDAL.update_kyluat(manv,yyyy, nam, ghichu, user) > 0)
                return true;
            return false;
        }

        public bool update_dsdukien(string manv, string mmyy, string hoten)
        {
            if (objDAL.update_dsdukien(manv, mmyy, hoten, user) > 0)
                return true;
            return false;
        }
        public bool update_dsdukien_thuongquy(string manv, string mmyy, string hoten)
        {
            if (objDAL.update_dsdukien_thuongquy(manv, mmyy, hoten, user) > 0)
                return true;
            return false;
        }
        public bool update_nv_tc(string manv, string mmyy, string tungay, string denngay, decimal songay, decimal hs_udcu, decimal hs_udmoi,string madv_tc, string ghichu)
        {
            if (objDAL.update_nv_tc(manv,mmyy,tungay,denngay,songay,hs_udcu,hs_udmoi,madv_tc,ghichu,user) > 0)
                return true;
            return false;
        }
        public bool del_nvtangcuong(string manv,string mmyy, string tungay)
        {
            sql = " delete from "+user+".nv_tangcuong_"+mmyy+" where manv='"+manv+"' and mmyy='"+mmyy+"'";
            sql += " and to_timestamp(to_char(tungay,'dd/mm/yyyy'),'dd/mm/yyyy')= to_timestamp('"+tungay.Trim()+"','dd/mm/yyyy')";
            if (objDAL.delete(sql) > 0)
                return true;
            return false;
        }
        public bool delete(string cmdtext)
        {
            if (objDAL.delete(cmdtext) > 0)
                return true;
            return false;
        }

        public bool update_khenthuong(DataSet ds,string yyyy)
        {
            int i = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                i = 0;
                sql = "Update " + user + ".khenthuong set madv='" + dr["madv"].ToString() + "',maloai='" + dr["maloai"].ToString() + "',ghichu='";
                sql += dr["ghichu"].ToString() + "',daunam=" + dr["daunam"].ToString() + ",cuoinam=" + dr["cuoinam"].ToString() + ",canam=";
                sql += dr["canam"].ToString() + ",so_kt=" + dr["so_kt"].ToString() + ",cs_kt=" + dr["cs_kt"].ToString() + ",ub_kt=" + dr["ub_kt"].ToString()+" ";
                sql += " ,giaykhen=" + dr["giaykhen"].ToString() + ",bangkhen=" + dr["bangkhen"].ToString() + " ";
                sql += " ,cstd_ct="+dr["cstd_ct"].ToString()+",bo_kt="+dr["bo_kt"].ToString()+",cp_kt="+dr["cp_kt"].ToString()+" ";
                sql += " ,cstd_tq="+dr["cstd_tq"].ToString()+",hc_h1="+dr["hc_h1"].ToString()+",hc_h2="+dr["hc_h2"].ToString()+" ";
                sql += " ,hc_h3="+dr["hc_h3"].ToString()+" ";
                sql += " where manv='" + dr["manv"].ToString() + "' and nam=" + dr["nam"].ToString();
                i = objDAL.Upd_data(sql);                
                if (i == 0)
                {
                    sql = "insert into " + user + ".khenthuong(manv,madv,maloai,ghichu,daunam,cuoinam,canam,so_kt,cs_kt,ub_kt ";
                    sql += " ,cstd_ct,cstd_tq,bo_kt,cp_kt,hc_h1,hc_h2,hc_h3,nam,giaykhen,bangkhen)";
                    sql += "values('" + dr["manv"].ToString() + "','" + dr["madv"].ToString() + "','" + dr["maloai"].ToString() + "','";
                    sql += dr["ghichu"].ToString() + "'," + dr["daunam"].ToString() + "," + dr["cuoinam"].ToString() + ",";
                    sql += dr["canam"].ToString() + "," + dr["so_kt"].ToString() + "," + dr["cs_kt"].ToString() + "," + dr["ub_kt"].ToString() + ","; 
                    sql += dr["cstd_ct"].ToString()+","+ dr["cstd_tq"].ToString()+","+ dr["bo_kt"].ToString()+",";
                    sql += dr["cp_kt"].ToString()+","+dr["hc_h1"].ToString()+","+dr["hc_h2"].ToString()+ ",";
                    sql += dr["hc_h3"].ToString()+ ",'"+ yyyy + "'," + dr["giaykhen"].ToString() + "," + dr["bangkhen"].ToString() + ")";
                    
                    i = objDAL.Upd_data(sql);
                }
            }
            return i > 0;
        }

        public bool update_khenthuong_bp(DataSet ds)
        {
            int i = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                i = 0;
                sql = " Update " + user + ".khenthuong_dv set ghichu='" + dr["ghichu"].ToString();
                sql += "' ,so_kt=" + dr["so_kt"].ToString() + ",cs_kt=" + dr["cs_kt"].ToString() + ",ub_kt=" + dr["ub_kt"].ToString();
                sql += " ,cstd_xs="+dr["cstd_xs"].ToString()+",bo_kt="+dr["bo_kt"].ToString()+"";
                sql += " ,cbo_kt="+dr["cbo_kt"].ToString()+",cp_kt="+dr["cp_kt"].ToString()+"";
                sql += " ,ccp_kt="+dr["ccp_kt"].ToString()+",hc_h1="+dr["hc_h1"].ToString()+"";
                sql += " ,hc_h2="+dr["hc_h2"].ToString()+",hc_h3="+dr["hc_h3"].ToString()+"";
                sql += " where madv='" + dr["madv"].ToString() + "' and nam=" + dr["nam"].ToString();
                i = objDAL.Upd_data(sql);
                
                if (i == 0)
                {
                    sql = "insert into " + user + ".khenthuong_dv(madv,nam,so_kt,cs_kt,ub_kt,cstd_xs,bo_kt,cbo_kt,cp_kt ";
                    sql +=" ,ccp_kt,hc_h1,hc_h2,hc_h3,ghichu)";
                    sql += "values('" + dr["madv"].ToString() + "'," + dr["nam"].ToString() + ",";
                    sql += dr["so_kt"].ToString() + "," + dr["cs_kt"].ToString() + "," + dr["ub_kt"].ToString() + ",";
                    sql += dr["cstd_xs"].ToString()+","+ dr["bo_kt"].ToString()+","+dr["cbo_kt"].ToString()+ ",";
                    sql += dr["cp_kt"].ToString()+","+dr["ccp_kt"].ToString()+","+dr["hc_h1"].ToString()+ ",";
                    sql += dr["hc_h2"].ToString()+","+dr["hc_h3"].ToString()+ ",'";
                    sql += dr["ghichu"].ToString() + "')";
                    i = objDAL.Upd_data(sql);
                }
            }
            return i > 0;
        }

        public bool InsertSotienKT(string nam,string t_dnam,string t_cnam,string t_nam,string ghichu)
        {
            int row = 0;
            sql = "insert into " + user + ".loaikt(nam,sotien_nam,sotien_dnam,sotien_cnam,ghichu) ";
            sql += "values(" + nam + "," + t_nam + "," + t_dnam + "," + t_cnam + ",'" + ghichu + "')";
            row = objDAL.Upd_data(sql);
            if (row < 1)
            {
                sql = "update " + user + ".loaikt set sotien_nam=" + t_nam + ",";
                sql += "sotien_dnam=" + t_dnam + ",sotien_cnam=" + t_cnam + ",ghichu='" + ghichu + "' ";
                sql += "where nam=" + nam;
                row = objDAL.Upd_data(sql);
            }
            return row > 0;
        }

        public bool nangluongtam(string manv,string hoten,string phai,string ngaysinh,string namsinh,string madv,
            string tenbp,string tenvt,string mangach,string loaingach,string b_dau,string hs_lc,string hs_vk,
            string ngaybnngach,string bac,string hs_lcmoi,string ngaynang,string luonghd,string user)
        {
            return objDAL.nangluongtam(manv, hoten, phai, ngaysinh, namsinh, madv, tenbp, tenvt, mangach, loaingach,
                b_dau, hs_lc, hs_vk, ngaybnngach, bac, hs_lcmoi, ngaynang, luonghd,user);
        }

        public bool ktratable(string mmyy)
        { 
            return objDAL.ktratable(mmyy,user);
        }

        public bool update_hs_luonghd(string manv, string hs_lc, string ngay, string bac, decimal lan)
        {
            string _ngay = ngay.Substring(6, 4) + "-" + ngay.Substring(3, 2) + "-" + ngay.Substring(0,2);
            string sql = "update " + user + ".hopdong set hs_lc=" + hs_lc + " ";
            sql += ",ngaybnngach=to_timestamp('" + _ngay + "','yyyy-mm-dd'),bac=" + bac + " where manv='" + manv + "' and lan=" + lan+" ";
            return Execute_data(sql);
        }

        public string Export_Excel(DataSet dset, string tenfile)
        {
            try
            {
                string dirPath = AppDomain.CurrentDomain.BaseDirectory + "Excel";
                string filePath = dirPath + "\\" + tenfile + ".xls";
                if (!System.IO.Directory.Exists(dirPath)) System.IO.Directory.CreateDirectory(dirPath);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false, System.Text.Encoding.Unicode);
                string astr = "";
                astr = "<Table>";//"<Table border=1>";
                astr = astr + "<tr>";
                for (int i = 0; i < dset.Tables[0].Columns.Count; i++)
                {
                    astr = astr + "<th>";
                    astr = astr + dset.Tables[0].Columns[i].ColumnName;
                    astr = astr + "</th>";
                }
                astr = astr + "</tr>";
                sw.Write(astr);
                for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
                {
                    astr = "<tr>";
                    for (int j = 0; j < dset.Tables[0].Columns.Count; j++)
                    {
                        astr = astr + "<td>";
                        astr = astr + dset.Tables[0].Rows[i][j].ToString();
                        astr = astr + "</td>";
                    }
                    astr = astr + "</tr>";
                    sw.Write(astr);
                }
                astr = "</Table>";
                sw.Write(astr);
                sw.Close();
                return filePath;
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Error : " + Ex, "Thông báo !");
                return "";
            }
        }

        public bool update_(string s_sql)
        {
          return  objDAL.update_(s_sql);
        }
        public string all_right()
        {
            return objDAL.all_right();
        }
        public bool upd_tdnnth(string ma, string ten, bool ngoaingu, bool tinhoc,string nhombc,int stt)
        {
            return objDAL.upd_tdnnth(ma, ten, ngoaingu, tinhoc,nhombc,stt, user);
        }
        public bool upd_madv(string manv, string madv, string mmyy)
        {
            sql = " update " + user + ".chamcong_"+mmyy+" set madv='" + madv + "' where manv='" + manv + "' and mmyy='" + mmyy + "'";
            try
            {
                Execute_data(sql);
                sql = sql.Replace("chamcong", "chamcong_thuongtruc");
                Execute_data(sql);
                sql = sql.Replace("chamcong_thuongtruc", "chamcong_tructc");
                Execute_data(sql);
                sql = sql.Replace("chamcong_tructc", "heso_nv");
                Execute_data(sql);
                sql = sql.Replace("heso_nv", "th_chamcong");
                Execute_data(sql);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public int update_bhxh(string manv, string hslc_cu, decimal hscv_cu, decimal hsvk_cu, decimal hstn_cu, decimal hskv_cu, decimal hs_lc, decimal hs_cv, decimal hs_vk, decimal hs_tnn, decimal hs_kv, string tuthang, string denthang, decimal tyle, bool trathe, string ghichu, string quy, string nam, string ngay)
        {
            return objDAL.update_bhxh(manv,hslc_cu,hscv_cu,hsvk_cu,hstn_cu,hskv_cu,hs_lc,hs_cv,hs_vk,hs_tnn,hs_kv,tuthang,denthang,tyle,trathe,ghichu,quy,nam,ngay,user);
        }
        // them thay doi password
        public bool update_pass(string userName, string pass)
        {
            if (objDAL.update_pass(userName, pass, user) > 0)
                return true;
            return false;
        }
        
        //them cham cong van tay
        public void del_vantay(string manv)
        {
            objDAL.Upd_data("delete from " + user + ".vantay where manv='" + manv + "'");
        }

        public string Upd_Image(string manv, byte[] temp1, byte[] temp2)
        {
            return objDAL.Upd_Image(manv, temp1, temp2, user);
        }
        public NpgsqlDataReader getRs()
        {
            return objDAL.getRs();
        }
        public NpgsqlDataReader Get_data_Reader(string sql)
        {
            return objDAL.Get_data_Reader(sql);
        }
        // lay thong tin benh vien
        public DataSet get_thongso()
        {
            sql = " select id,ten from " + user + ".thongso where id =3 "; // id=3 la benh vien ha giang
            return objDAL.get_data(sql);
        }
        // lay thong tin danh muc
        public DataSet get_bophan()
        {
            sql = " select * from " + user + ".bophan order by sothutu,madv";
            return objDAL.get_data(sql);
        }
        public DataSet get_chucvu()
        {
            sql = " select * from " + user + ".dmcv order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_chucdanh()
        {
            sql = " select * from " + user + ".dmcd order by thutu";
            return objDAL.get_data(sql);
        }
        public int sothang_hd(string mahd)
        {
            sql = " select sothang from "+user+".mahd where mahd='"+mahd.Trim()+"'";
            return Int32.Parse(objDAL.get_data(sql).Tables[0].Rows[0]["sothang"].ToString());
        }
        public DataSet get_hopdong()
        {
            sql = " select * from " + user + ".mahd order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_loaihd_bantg()
        {
            sql = " select * from " + user + ".loaihd_bantg order by ma";
            return objDAL.get_data(sql);
        }
        public DataSet get_hocvan()
        {
            sql = " select * from " + user + ".hocvan order by mahv";
            return objDAL.get_data(sql);
        }
        public DataSet get_tinhthanh()
        {
            sql = " select * from " + user + ".btdtt order by matt";
            return objDAL.get_data(sql);
        }
        public DataSet get_quanhuyen()
        {
            sql = " select * from " + user + ".btdquan order by matt";
            return objDAL.get_data(sql);
        }
        public DataSet get_phuongxa()
        {
            sql = " select * from " + user + ".btdpxa order by maqu";
            return objDAL.get_data(sql);
        }
        public DataSet get_benhvien()
        {
            sql = " select * from " + user + ".benhvien order by mabv";
            return objDAL.get_data(sql);
        }
        public DataSet get_ctdaotao()
        {
            sql = " select * from " + user + ".ctdaotao order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_tongiao()
        {
            sql = " select * from " + user + ".tongiao order by matgiao";
            return objDAL.get_data(sql);
        }
        public DataSet get_dantoc()
        {
            sql = " select * from " + user + ".dantoc order by madt";
            return objDAL.get_data(sql);
        }
        public DataSet get_tuyendung()
        {
            sql = " select * from " + user + ".tuyendung order by matd";
            return objDAL.get_data(sql);
        }
        public DataSet get_ngoaingu()
        {
            sql = " select * from " + user + ".ngoaingu order by mann";
            return objDAL.get_data(sql);
        }
        public DataSet get_tdnnth()
        {
            sql = " select * from " + user + ".tdnnth order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_quocgia()
        {
            sql = " select * from " + user + ".quocgia order by maqg";
            return objDAL.get_data(sql);
        }
        public DataSet get_mangach()
        {
            sql = " select * from " + user + ".mangach order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_loaingach()
        {
            sql = " select * from " + user + ".loaingach order by id";
            return objDAL.get_data(sql);
        }
        public DataSet get_nhomngach()
        {
            sql = " select * from " + user + ".nhomngach order by manhom";
            return objDAL.get_data(sql);
        }

        public DataSet get_nhomngach(string manhom)
        {
            sql = " select * from " + user + ".nhomngach where manhom='"+manhom.Trim()+"' order by manhom";
            return objDAL.get_data(sql);
        }

        public DataSet get_nhombc()
        {
            sql = " select * from " + user + ".nhombc order by manhom";
            return objDAL.get_data(sql);
        }
        public DataSet get_nhomctdt()
        {
            sql = " select * from " + user + ".dmnhomctdt order by manhom";
            return objDAL.get_data(sql);
        }
        public DataSet get_doituong()
        {
            sql = " select * from " + user + ".doituong order by madt";
            return objDAL.get_data(sql);
        }

        public DataSet get_dmlydo_phep()
        {
            sql = " select * from " + user + ".dmlydo_phep order by stt";
            return objDAL.get_data(sql);
        }
        public DataSet get_dmlydo()
        {
            sql = " select * from " + user + ".dmlydo order by ma";
            return objDAL.get_data(sql);
        }
        public DataSet get_bangcap()
        {
            sql = " select * from " + user + ".bangcap order by stt";
            return objDAL.get_data(sql);
        }
        // moi them
        public DataSet get_dmchibo()
        {
            sql = " select * from "+user +".dmchibo order by stt";
            return objDAL.get_data(sql);
        }
        //
        public DataSet get_hedaotao()
        {
            sql = " select * from " + user + ".hedaotao order by mahe";
            return objDAL.get_data(sql);
        }
        public DataSet get_hocham()
        {
            sql = " select * from " + user + ".hocham order by mahocham";
            return objDAL.get_data(sql);
        }
        public DataSet get_dmphai()
        {
            sql = " select * from " + user + ".dmphai";
            return objDAL.get_data(sql);
        }
        public DataSet get_dmheso()
        {
            sql = " select * from " + user + ".dmheso order by heso";
            return objDAL.get_data(sql);
        }
        public DataSet get_tdqlnn()
        {
            sql = " select * from " + user + ".tdqlnn order by ma";
            return objDAL.get_data(sql);
        }
        public DataSet get_dmdtctri()
        {
            sql = " select * from " + user + ".dmdtctri order by ten";
            return objDAL.get_data(sql);
        }
        public DataSet get_heso(string mmyy)
        {
            sql = " select * from " + user + ".heso where mmyy='"+mmyy+"' order by mmyy";
            return objDAL.get_data(sql);
        }
        //lay danh sach madv theo khu vuc thuong
        public string get_madv_theokv(string khuvuc)
        {
            string str = "";
            sql = " select madv from "+user+".bophan where khuvuc='"+khuvuc+"'";
            DataSet ds = objDAL.get_data(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["madv"].ToString().Trim() != "")
                        str += ""+dr["madv"].ToString().Trim()+",";
                }
            }
            if (str.Trim() != "")
            {
                str = str.Substring(0, str.Length - 1);
            }
            return str;
        }
        //lay danh sach ngay nghi le hoac ngay bu le trong thang
        public DataSet get_ngaynghile(int loai,string s_mmyy)
        {
            sql = " select to_char(ngay,'dd/mm/yyyy') as ngay from "+user+".dmngayle where loai="+loai+" and mmyy='"+s_mmyy+"' ";
            return objDAL.get_data(sql);
        }
        //thanh tung 01/07/2011
        public bool update_chamcong_thuongtruc(string manv, string madv, string mmyy, decimal tt, decimal tn, decimal tl, decimal ht, decimal hn, decimal hl,decimal sntru_tt)
        {
            return objDAL.update_chamcong_thuongtruc(manv,madv, mmyy,tt,tn,tl,ht,hn,hl,sntru_tt,user);
        }
        // thanh tung 02/07/2011
        public bool update_chamcong_ngoaigio(string manv, string madv, string mmyy, decimal nng_ngaythuong, decimal nng_chunhat, decimal nng_letet, decimal dng_ngaythuong, decimal dng_chunhat, decimal dng_letet)
        {
            return objDAL.update_chamcong_ngoaigio(manv, madv, mmyy, nng_ngaythuong, nng_chunhat, nng_letet, dng_ngaythuong, dng_chunhat, dng_letet, user);
        }
        public bool udp_ccng(string manv, string mmyy, string ngay, DateTime batdau, DateTime ketthuc, string ngaybatdau, string ngayketthuc, bool hscc, bool t7cn, bool letet, decimal ghichu)
        {
            return objDAL.upd_ccng(manv, mmyy, ngay, batdau, ketthuc, ngaybatdau, ngayketthuc, hscc, letet, t7cn, ghichu, user);
        }
        // bao cao benh vien tuyen quang
        public DataSet BC_chatluongcbcv(string ngay, string thang, string nam, string bophan)
        {
            sql=" select bp.madv,bp.tenbp,count(l.manv) as tongso";
            sql+=" ,sum( case when upper(hd.dang)='R' then 1 else 0 end ) as tsdang";
            sql+=" ,sum( case when l.phai<>'Nam' then 1 else 0 end ) as tsnvnu";
            sql+=" ,sum( case when l.madt<>'25' then 1 else 0 end ) as tsnvdt";
            sql+=" ,sum( case when mn.nhomngach in ('CC') then 1 else 0 end ) as tscvcc";
            sql+=" ,sum( case when mn.nhomngach in ('VC') then 1 else 0 end ) as tscvc";
            sql+=" ,sum( case when mn.nhomngach in ('CV') then 1 else 0 end ) as tscv";
            sql+=" ,sum( case when mn.nhomngach in ('CS') then 1 else 0 end ) as tscs";
            sql+=" ,sum( case when mn.nhomngach in ('NV') then 1 else 0 end ) as tsnv";
            sql+=" ,sum( case when l.td_nnghiep in ('01') then 1 else 0 end ) as tstiensy";
            sql+=" ,sum( case when l.td_nnghiep in ('02') then 1 else 0 end ) as tsthacsy";
            sql+=" ,sum( case when l.td_nnghiep in ('07') then 1 else 0 end ) as tsckI";
            sql+=" ,sum( case when l.td_nnghiep in ('08') then 1 else 0 end ) as tsckII";
            sql+=" ,sum( case when bc.mabc in ('DH') then 1 else 0 end ) as tsdh";
            sql+=" ,sum( case when bc.mabc in ('CD') then 1 else 0 end ) as tscd";
            sql+=" ,sum( case when bc.mabc in ('TH') then 1 else 0 end ) as tsth";
            sql+=" ,sum( case when bc.mabc in ('SH') then 1 else 0 end ) as tssh";
            sql+=" ,sum( case when bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tskhac";
            sql+=" ,sum( case when l.chinhtri in ('CC') then 1 else 0 end ) as ctricc";
            sql+=" ,sum( case when l.chinhtri in ('CN') then 1 else 0 end ) as ctricn";
            sql+=" ,sum( case when l.chinhtri in ('TC') then 1 else 0 end ) as ctritc";
            sql+=" ,sum( case when l.chinhtri in ('SC') then 1 else 0 end ) as ctrisc";
            sql+=" ,sum( case when nt.nhombc in ('DH','CN','VB') then 1 else 0 end ) as thvanbang";
            sql+=" ,sum( case when nt.nhombc in ('CC') then 1 else 0 end ) as thchungchi";
            sql+=" ,sum( case when l.mann='02' or l.mann1='02' or l.mann2='02' or l.mann3='02' then 1 else 0 end ) as nn_anh";
            sql+=" ,sum( case when l.mann='03' or l.mann1='03' or l.mann2='03' or l.mann3='03' then 1 else 0 end ) as nn_phap";
            sql+=" ,sum( case when l.mann='07' or l.mann1='07' or l.mann2='07' or l.mann3='07' then 1 else 0 end ) as nn_nga";
            sql+=" ,sum( case when l.mann not in ('02','03','07','') or l.mann1 not in ('02','03','07','') or l.mann2 not in ('02','03','07','') or l.mann3 not in ('02','03','07','') then 1 else 0 end ) as nn_khac";
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) < 30 then 1 else 0 end ) as t_duoi30";
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 30  and extract(year from age(l.ngaysinh)) <40 then 1 else 0 end ) as t_30den40";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 40  and extract(year from age(l.ngaysinh)) <50 then 1 else 0 end ) as t_40den50";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 50  and extract(year from age(l.ngaysinh)) <55 then 1 else 0 end ) as t_50den55";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 55  and extract(year from age(l.ngaysinh)) <=60 and l.phai='Nam' then 1 else 0 end ) as t_nam55den60";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 55  and extract(year from age(l.ngaysinh)) <=60 and l.phai='Nữ' then 1 else 0 end ) as t_nu55den60";    
            sql+=" ,'' as ghichu";    
            sql+=" from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";    
            sql+=" left join "+user+".bophan bp on l.madv=bp.madv";    
            sql+=" left join "+user+".mangach mn on hd.mangach=mn.ma";    
            sql+=" left join "+user+".bangcap bc on l.td_nnghiep=bc.mabangcap";    
            sql+=" left join "+user+".tdnnth nt on l.tdth=nt.ma";
            sql+=" inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd') <= to_timestamp('"+nam+"-"+thang+"-"+ngay+"','yyyy-mm-dd')";
            if (bophan != "")
                sql += " and l.madv='"+bophan.Trim()+"'";
            sql+=" group by bp.madv,bp.tenbp,bp.sothutu";
            sql += " order by bp.sothutu";
            return objDAL.get_data(sql);
        }
        public DataSet BC_chatluongcbcvmau2(string ngay, string thang, string nam, string bophan)
        {
            sql=" select bp.madv,bp.tenbp,count(l.manv) as tongso";
            sql+=" ,sum( case when l.phai='Nữ' then 1 else 0 end ) as tsnvnu";
            sql+=" ,0.00 as tyle_nu";    
            sql+=" ,sum( case when l.madt<>'25' then 1 else 0 end ) as tsnvdt";    
            sql+=" ,0.00 as tyle_dt";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) < 30 then 1 else 0 end ) as t_duoi30";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 30  and extract(year from age(l.ngaysinh)) <40 then 1 else 0 end ) as t_30den40";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 40  and extract(year from age(l.ngaysinh)) <50 then 1 else 0 end ) as t_40den50";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 50  and extract(year from age(l.ngaysinh)) <55 then 1 else 0 end ) as t_50den55";    
            sql+=" ,sum( case when extract(year from age(l.ngaysinh)) >= 55  and extract(year from age(l.ngaysinh)) <=60 then 1 else 0 end ) as t_55den60";    
            sql+=" ,sum( case when bc.mabc in ('TD') then 1 else 0 end ) as tstdh";    
            sql+=" ,sum( case when bc.mabc in ('DH') then 1 else 0 end ) as tsdh";    
            sql+=" ,sum( case when bc.mabc in ('CD') then 1 else 0 end ) as tscd";    
            sql+=" ,sum( case when bc.mabc in ('TH') then 1 else 0 end ) as tsth";    
            sql+=" ,sum( case when bc.mabc not in ('TD','DH','CD','TH') then 1 else 0 end ) as tstdkhac";    
            sql+=" ,sum( case when l.he_dt in ('01') then 1 else 0 end ) as chinhquy";    
            sql+=" ,sum( case when l.he_dt in ('05') then 1 else 0 end ) as chuyentu";    
            sql+=" ,sum( case when l.he_dt in ('03') then 1 else 0 end ) as taichuc";    
            sql+=" ,sum( case when l.chinhtri in ('CC') then 1 else 0 end ) as ctricc";    
            sql+=" ,sum( case when l.chinhtri in ('CN') then 1 else 0 end ) as ctricn";    
            sql+=" ,sum( case when l.chinhtri in ('TC') then 1 else 0 end ) as ctritc";    
            sql+=" ,sum( case when l.chinhtri in ('SC') then 1 else 0 end ) as ctrisc";    
            sql+=" ,sum( case when nt.nhombc in ('DH','CN','VB') then 1 else 0 end ) as thvanbang";    
            sql+=" ,sum( case when nt.nhombc in ('CC') then 1 else 0 end ) as thchungchi";    
            sql+=" ,sum( case when l.mann='02' or l.mann1='02' or l.mann2='02' or l.mann3='02' then 1 else 0 end ) as nn_anh";    
            sql+=" ,sum( case when l.mann not in ('02','') or l.mann1 not in ('02','') or l.mann2 not in ('02','') or l.mann3 not in ('02','') then 1 else 0 end ) as nn_khac";    
            sql+=" ,'' as xuatsac,'' as hoanthanhtot,'' as hoanthanh,'' as chuahoanthanh,'' as ghichu";    
            sql+=" from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql+=" left join "+user+".bophan bp on l.madv=bp.madv";   
            sql+=" left join "+user+".mangach mn on hd.mangach=mn.ma";    
            sql+=" left join "+user+".bangcap bc on l.td_nnghiep=bc.mabangcap";    
            sql+=" left join "+user+".tdnnth nt on l.tdth=nt.ma";    
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd') <= to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd')";
            if (bophan != "")
                sql += " and l.madv='" + bophan.Trim() + "'";
            sql += " group by bp.madv,bp.tenbp,bp.sothutu";
            sql += " order by bp.sothutu";
            return objDAL.get_data(sql);
        }
        public DataSet BC_cocaucbcnvc(string ngay, string thang, string nam, string bophan)
        {
            sql = " select '"+Maincode("Tenbv")+"' as tenbv,count(l.manv) as tongso";
            sql += " ,sum( case when mn.nhomngach in ('CC') then 1 else 0 end ) as tscvcc";
            sql += " ,sum( case when mn.nhomngach in ('VC') then 1 else 0 end ) as tscvc";
            sql += " ,sum( case when mn.nhomngach in ('CV') then 1 else 0 end ) as tscv";
            sql += " ,sum( case when mn.nhomngach in ('CS') then 1 else 0 end ) as tscs";
            sql += " ,sum( case when mn.nhomngach in ('NV') then 1 else 0 end ) as tsnv";
            sql += " ,sum( case when dt.manhom in ('01','04','05','06','07','08','09','10') and l.td_nnghiep in ('01') then 1 else 0 end ) as tstiensy";
            sql += " ,sum( case when dt.manhom in ('01','04','05','06','07','08','09','10') and l.td_nnghiep in ('02') then 1 else 0 end ) as tsthacsy";
            sql += " ,sum( case when dt.manhom in ('01','04','05','06','07','08','09','10') and l.td_nnghiep in ('07') then 1 else 0 end ) as tsckI";
            sql += " ,sum( case when dt.manhom in ('01','04','05','06','07','08','09','10') and l.td_nnghiep in ('08') then 1 else 0 end ) as tsckII ";
            sql += " ,sum( case when dt.manhom in ('01') and bc.mabc in ('DH') then 1 else 0 end ) as bacsy ";
            sql += " ,sum( case when dt.manhom in ('06') and bc.mabc in ('DH') then 1 else 0 end ) as dd_dh";
            sql += " ,sum( case when dt.manhom in ('04') and bc.mabc in ('DH') then 1 else 0 end ) as ktv_dh";
            sql += " ,sum( case when dt.manhom in ('08') and bc.mabc in ('DH') then 1 else 0 end ) as nhs_dh";
            sql += " ,sum( case when dt.manhom in ('06') and bc.mabc in ('CD') then 1 else 0 end ) as dd_cd";
            sql += " ,sum( case when dt.manhom in ('04') and bc.mabc in ('CD') then 1 else 0 end ) as ktv_cd";
            sql += " ,sum( case when dt.manhom in ('08') and bc.mabc in ('CD') then 1 else 0 end ) as nhs_cd";
            sql += " ,sum( case when dt.manhom in ('06') and bc.mabc in ('TH') then 1 else 0 end ) as dd_th";
            sql += " ,sum( case when dt.manhom in ('04') and bc.mabc in ('TH') then 1 else 0 end ) as ktv_th";
            sql += " ,sum( case when dt.manhom in ('08') and bc.mabc in ('TH') then 1 else 0 end ) as nhs_th";
            sql += " ,sum( case when dt.manhom in ('06') and bc.mabc in ('SH') then 1 else 0 end ) as dd_sh";
            sql += " ,sum( case when dt.manhom in ('09') and bc.mabc in ('SH') then 1 else 0 end ) as holy_sh";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc in ('TD') then 1 else 0 end ) as tstdh";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc in ('DH') then 1 else 0 end ) as tsdh";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc in ('CD') then 1 else 0 end ) as tscd";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc in ('TH') then 1 else 0 end ) as tsth";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc in ('SH') then 1 else 0 end ) as tssh";
            sql += " ,sum( case when dt.manhom not in ('01','04','05','06','07','08','09','10') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tstdkhac";
            sql += " ,'' as ghicchu";
            sql += " from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " left join "+user+".bophan bp on l.madv=bp.madv";
            sql += " left join "+user+".mangach mn on hd.mangach=mn.ma";
            sql += " left join "+user+".bangcap bc on l.td_nnghiep=bc.mabangcap";
            sql += " left join "+user+".tdnnth nt on l.tdth=nt.ma";
            sql += " left join "+user+".ctdaotao dt on l.nnghiep=dt.mactdt";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd') <= to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd')";
            return objDAL.get_data(sql);
        }
        public DataSet BC_thongkecocaucb(string ngay, string thang, string nam, string bophan)
        {
            sql = " select bp.madv,bp.tenbp";
            sql += " ,sum( case when upper(hd.dang)='R' and mh.nhombc=1 then 1 else 0 end ) as tsdang";
            sql += " ,sum( case when mh.nhombc=1 then 1 else 0 end) as tongso_bc";
            sql += " ,sum( case when mh.nhombc=1 and l.phai='Nữ' then 1 else 0 end ) as tsnvnu_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and l.td_nnghiep in ('01') then 1 else 0 end ) as tstiensy_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and l.td_nnghiep in ('02') then 1 else 0 end ) as tsthacsy_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and l.td_nnghiep in ('07') then 1 else 0 end ) as tsckI_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and l.td_nnghiep in ('08') then 1 else 0 end ) as tsckII_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01') and bc.mabc in ('DH') then 1 else 0 end ) as bacsy_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('CD') then 1 else 0 end ) as caodang_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('TH') then 1 else 0 end ) as tsth_bc ";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('SH') then 1 else 0 end ) as tssh_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tstdkhac_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('TD') then 1 else 0 end ) as nktdh_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('DH') then 1 else 0 end ) as nkdh_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('CD') then 1 else 0 end ) as nkcd_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('TH') then 1 else 0 end ) as nkth_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('SH') then 1 else 0 end ) as nksh_bc";
            sql += " ,sum( case when mh.nhombc=1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as nkkhac_bc";
            sql += " ,sum( case when mh.nhombc <>1 then 1 else 0 end) as tongso_hd";
            sql += " ,sum( case when mh.nhombc <>1 and l.phai='Nữ' then 1 else 0 end ) as tsnvnu_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('TD') then 1 else 0 end ) as tstdh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('DH') then 1 else 0 end ) as tsdh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('CD') then 1 else 0 end ) as tscd_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('TH') then 1 else 0 end ) as tsth_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc in ('SH') then 1 else 0 end ) as tssh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom in ('01','04','05','06','07','08','10') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tstdkhac_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('DH') then 1 else 0 end ) as nkdh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('TD') then 1 else 0 end ) as nktdh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('CD') then 1 else 0 end ) as nkcd_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('TH') then 1 else 0 end ) as nkth_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc in ('SH') then 1 else 0 end ) as nksh_hd";
            sql += " ,sum( case when mh.nhombc <>1 and dt.manhom not in ('01','04','05','06','07','08','10') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as nkkhac_hd";
            sql += " from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " left join "+user+".bophan bp on l.madv=bp.madv";
            sql += " left join "+user+".mangach mn on hd.mangach=mn.ma";
            sql += " left join "+user+".bangcap bc on l.td_nnghiep=bc.mabangcap";
            sql += " left join "+user+".ctdaotao dt on l.nnghiep=dt.mactdt";
            sql += " left join "+user+".mahd mh on hd.loaihd=mh.mahd";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd') <= to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd')";
            if (bophan != "")
                sql += " and l.madv='" + bophan.Trim() + "'";
            sql += " group by bp.madv,bp.tenbp,bp.sothutu";
            sql += " order by bp.sothutu";
            return objDAL.get_data(sql);
        }
        public DataSet BC_thongkecbtheococauchuyenmon(string ngay, string thang, string nam, string bophan)
        {
            sql = " select bp.madv,bp.tenbp,count(l.manv) as tongso";
            sql += " ,sum( case when bp.mabsoft in ('L') then 1 else 0 end) as tongso_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and l.phai='Nữ' then 1 else 0 end ) as tsnvnu_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and upper(hd.dang)='R' then 1 else 0 end ) as tsdang_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and l.td_nnghiep in ('01') then 1 else 0 end ) as tstiensy_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and l.td_nnghiep in ('02') then 1 else 0 end ) as tsthacsy_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and l.td_nnghiep in ('07') then 1 else 0 end ) as tsckI_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and l.td_nnghiep in ('08') then 1 else 0 end ) as tsckII_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and bc.mabc in ('DH') then 1 else 0 end ) as bacsy_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and bc.mabc in ('CD') then 1 else 0 end ) as caodang_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and bc.mabc in ('TH') then 1 else 0 end ) as trunghoc_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and bc.mabc in ('SH') then 1 else 0 end ) as sohoc_ls";
            sql += " ,sum( case when bp.mabsoft in ('L') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tdkhac_ls";
            sql += " ,sum( case when bp.mabsoft in ('C') then 1 else 0 end) as tongso_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and l.phai='Nữ' then 1 else 0 end ) as tsnvnu_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and upper(hd.dang)='R' then 1 else 0 end ) as tsdang_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and l.td_nnghiep in ('01') then 1 else 0 end ) as tstiensy_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and l.td_nnghiep in ('02') then 1 else 0 end ) as tsthacsy_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and l.td_nnghiep in ('07') then 1 else 0 end ) as tsckI_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and l.td_nnghiep in ('08') then 1 else 0 end ) as tsckII_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and bc.mabc in ('DH') then 1 else 0 end ) as bacsy_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and bc.mabc in ('CD') then 1 else 0 end ) as caodang_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and bc.mabc in ('TH') then 1 else 0 end ) as trunghoc_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and bc.mabc in ('SH') then 1 else 0 end ) as sohoc_cls";
            sql += " ,sum( case when bp.mabsoft in ('C') and bc.mabc not in ('TD','DH','CD','TH','SH') then 1 else 0 end ) as tdkhac_cls";
            sql += " from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " left join "+user+".bophan bp on l.madv=bp.madv";
            sql += " left join "+user+".mangach mn on hd.mangach=mn.ma";
            sql += " left join "+user+".bangcap bc on l.td_nnghiep=bc.mabangcap";
            sql += " left join "+user+".ctdaotao dt on l.nnghiep=dt.mactdt";
            sql += " left join "+user+".mahd mh on hd.loaihd=mh.mahd";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan ";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd') <= to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd')";
            if (bophan != "")
                sql += " and l.madv='" + bophan.Trim() + "'";
            sql += " group by bp.madv,bp.tenbp,bp.mabsoft";
            sql += " order by bp.mabsoft";
            return objDAL.get_data(sql);
        }
        public DataSet BC_thongtinngaysinhnhanvien(string tungay,string denngay,string bophan)
        {
            string  ngayht="", nam = "",tn="",dn="";            
            ngayht = DateTime.Now.ToString("dd/MM/yyyy");
            nam = ngayht.Substring(6,4);
          
            tn = tungay.Substring(0, 5).ToString();
            dn = denngay.Substring(0,5).ToString();
            sql = " select l.manv,trim(l.ho)||' '|| trim(l.ten) as hoten,bp.madv,bp.tenbp,to_char(l.ngaysinh,'dd-mm-yyyy') as ngaysinh";
            sql += " , extract(year from age(to_timestamp('" + nam + "','yyyy'),to_timestamp(to_char(l.ngaysinh,'yyyy'),'yyyy'))) as sotuoi";
            sql += " , trim(l.noio)||' ,'||trim(qu.tenquan)||' ,'||trim(tt.tentt) as noio";
            sql += " ,case when dtdd ='' then dienthoai else dtdd end as dienthoai,bp.sothutu as stt_bp";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " left join " + user + ".bophan bp on l.madv=bp.madv";
            sql += " left join " + user + ".btdtt tt on l.t_noio=tt.matt";
            sql += " left join " + user + ".btdquan qu on l.h_noio=qu.maqu";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null and hd.lan=ml.lan";
            sql += " and to_timestamp(to_char(hd.ngaydenct,'dd/MM/yyyy'),'dd/MM/yyyy') <= to_timestamp('" + ngayht + "','dd/MM/yyyy')";
            sql += " and to_timestamp(to_char(l.ngaysinh,'dd/MM'),'dd/MM') between to_timestamp('" + tn + "','dd/MM') ";
            sql += "  and to_timestamp('" + dn + "','dd/MM')";
            if (bophan != "")
                sql += " and l.madv='" + bophan.Trim() + "'";
                     
            sql+=" order by bp.sothutu";
            return objDAL.get_data(sql);
        }
        public string get_ngaytrongthang(string thang, string nam)
        {
            int ngay=0,t = 0, n = 0;            
            t = Int32.Parse(thang);
            if (nam != "")
            {
                n = Int32.Parse(nam);
            }
            switch (t)
            {
                case 1: ngay = 31;
                    break;
                case 2: if (n % 4 == 0) ngay = 29; else ngay = 28;
                    break;
                case 3: ngay = 31;
                    break;
                case 4: ngay = 30;
                    break;
                case 5: ngay = 31;
                    break;
                case 6: ngay = 30;
                    break;
                case 7: ngay = 31;
                    break;
                case 8: ngay = 31;
                    break;
                case 9: ngay = 30;
                    break;
                case 10: ngay = 31;
                    break;
                case 11: ngay = 30;
                    break;
                case 12: ngay = 31;
                    break;
            }
            return ngay.ToString().PadLeft(2, '0');
        }
        // lay so ngay trong thang tra ve kieu so
        public int get_ngaytrongthang(int thang,int nam)
        {
            int ngay = 0, t = 0, n = 0;
            t = thang; n = nam;
            switch (t)
            {
                case 1: ngay = 31;
                    break;
                case 2: if (n % 4 == 0) ngay = 29; else ngay = 28;
                    break;
                case 3: ngay = 31;
                    break;
                case 4: ngay = 30;
                    break;
                case 5: ngay = 31;
                    break;
                case 6: ngay = 30;
                    break;
                case 7: ngay = 31;
                    break;
                case 8: ngay = 31;
                    break;
                case 9: ngay = 30;
                    break;
                case 10: ngay = 31;
                    break;
                case 11: ngay = 30;
                    break;
                case 12: ngay = 31;
                    break;
            }
            return ngay;
        }       
        // lay ngay cuoi thang kieu dd/mm/yyyy        
        public string get_ncuoithangddmmyyyy(string thang, string nam)
        {
            int ngay = 0, t = 0, n = 0;
            t = Int32.Parse(thang);
            if (nam != "")
            {
                n = Int32.Parse(nam);
            }
            switch (t)
            {
                case 1: ngay = 31;
                    break;
                case 2: if (n % 4 == 0) ngay = 29; else ngay = 28;
                    break;
                case 3: ngay = 31;
                    break;
                case 4: ngay = 30;
                    break;
                case 5: ngay = 31;
                    break;
                case 6: ngay = 30;
                    break;
                case 7: ngay = 31;
                    break;
                case 8: ngay = 31;
                    break;
                case 9: ngay = 30;
                    break;
                case 10: ngay = 31;
                    break;
                case 11: ngay = 30;
                    break;
                case 12: ngay = 31;
                    break;
            }
            return ngay.ToString().Trim()+"/"+thang+ "/"+nam;
        }
        public bool upd_hscv(string macv, decimal heso)
        {
            return objDAL.upd_hsccv(macv, heso, user);
        }
        public DataSet get_nghiphep(string p_nam,string s_mmyy)
        {
            string tam = "", sql = "select ll.manv,(pn.phep+pn.phepthem+pn.ts_ngayton) - (sum(";
            sql += "case when n1='PN' then 1 when n1='P2' then 0.5 else 0 end + case when n2='PN' then 1 when n2='P2' then 0.5 else 0 end";
            sql += "+ case when n3='PN' then 1 when n3='P2' then 0.5 else 0 end + case when n4='PN' then 1 when n4='P2' then 0.5 else 0 end";
            sql += "+ case when n5='PN' then 1 when n5='P2' then 0.5 else 0 end + case when n6='PN' then 1 when n6='P2' then 0.5 else 0 end";
            sql += "+ case when n7='PN' then 1 when n7='P2' then 0.5 else 0 end + case when n8='PN' then 1 when n8='P2' then 0.5 else 0 end";
            sql += "+ case when n9='PN' then 1 when n9='P2' then 0.5 else 0 end + case when n10='PN' then 1 when n10='P2' then 0.5 else 0 end";
            sql += "+ case when n11='PN' then 1 when n11='P2' then 0.5 else 0 end + case when n12='PN' then 1 when n12='P2' then 0.5 else 0 end";
            sql += "+ case when n13='PN' then 1 when n13='P2' then 0.5 else 0 end + case when n14='PN' then 1 when n14='P2' then 0.5 else 0 end";
            sql += "+ case when n15='PN' then 1 when n15='P2' then 0.5 else 0 end + case when n16='PN' then 1 when n16='P2' then 0.5 else 0 end";
            sql += "+ case when n17='PN' then 1 when n17='P2' then 0.5 else 0 end + case when n18='PN' then 1 when n18='P2' then 0.5 else 0 end";
            sql += "+ case when n19='PN' then 1 when n19='P2' then 0.5 else 0 end + case when n20='PN' then 1 when n20='P2' then 0.5 else 0 end";
            sql += "+ case when n21='PN' then 1 when n21='P2' then 0.5 else 0 end + case when n22='PN' then 1 when n22='P2' then 0.5 else 0 end";
            sql += "+ case when n23='PN' then 1 when n23='P2' then 0.5 else 0 end + case when n24='PN' then 1 when n24='P2' then 0.5 else 0 end";
            sql += "+ case when n25='PN' then 1 when n25='P2' then 0.5 else 0 end + case when n26='PN' then 1 when n26='P2' then 0.5 else 0 end";
            sql += "+ case when n27='PN' then 1 when n27='P2' then 0.5 else 0 end + case when n28='PN' then 1 when n28='P2' then 0.5 else 0 end";
            sql += "+ case when n29='PN' then 1 when n29='P2' then 0.5 else 0 end + case when n30='PN' then 1 when n30='P2' then 0.5 else 0 end";
            sql += "+ case when n31='PN' then 1 when n31='P2' then 0.5 else 0 end)+ (t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12)) as phep_con ";
            sql += " from " + user + ".chamcong_"+s_mmyy.Trim()+" cc inner join " + user + ".llnv ll on ll.manv=cc.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " inner join "+user+".phepnam_"+p_nam.Trim()+" pn on ll.manv=pn.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan";
            sql += " group by ll.manv,pn.phep,pn.phepthem,pn.ts_ngayton,pn.t1,pn.t2,pn.t3,pn.t4,pn.t5,pn.t6,pn.t7,pn.t8,pn.t9,pn.t10,pn.t11,pn.t12";
            return objDAL.get_data(sql);
        }
        public DataSet get_ngaytruc(string s_mmyy,int ngay_cc)
        {
            string sql_t = "";
            string sql = "select ll.manv,cc_tt.ts_ngayton + sum(";
            for (int i = 1; i <= ngay_cc; i++)
            {
                sql_t += " case when n"+i.ToString()+" in ('TT','TN','TL','HT','HN','HL') then 1 else 0 end +";
            }
            sql_t = sql_t.Trim().Substring(0, sql_t.Trim().Length - 1);
            sql += sql_t;
            sql += " ) as ts_ngaytruc ";
            sql += " from " + user + ".chamcong_thuongtruc_"+s_mmyy.Trim()+" cc_tt inner join " + user + ".llnv ll on ll.manv=cc_tt.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " where cc_tt.mmyy='" + s_mmyy.Trim() + "'";
            sql += " group by ll.manv,cc_tt.ts_ngayton";
            return objDAL.get_data(sql);
        }
        
        public DataSet get_nlcuoituan(string s_mmyy, string thang, string nam, int ngay_cc)
        {
            string str = ngaycuoituan(thang, nam, ngay_cc);
            if (str.Length > 0)
                str = str.Trim().Substring(0, str.Length - 1);
            string[] arr = str.Split(',');
            string sql = "select ll.manv,cc.ts_ntonhc,0.0 as ts_nlcuoituan, ";
            string sql_t = "";
            if (str.Trim().Length > 0)
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    sql_t += " n" + arr[i].ToString() + ",";
                }
                sql += sql_t.Trim().Substring(0, sql_t.Trim().Length - 1);
            }
            else
            {
                sql += " 0";
            }
            sql += " from " + user + ".chamcong_" + s_mmyy.Trim() + " cc inner join " + user + ".llnv ll on ll.manv=cc.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null and cc.mmyy='" + s_mmyy.Trim() + "'";
            sql += " order by ll.manv";
            ds= objDAL.get_data(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    decimal ts_nlcuoituan = 0;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (dr["n" + arr[i].ToString() + ""].ToString().Trim().Length > 2)
                        {
                            string[] arr1 = dr["n" + arr[i].ToString() + ""].ToString().Trim().Split('/');
                            if (arr1[0].Trim() == "X" || arr1[1].Trim() == "X")
                            {
                                ts_nlcuoituan += 1;
                            }
                            if (arr1[0].Trim() == "X2" || arr1[1].Trim() == "X2")
                            {
                                ts_nlcuoituan += decimal.Parse("0.5");
                            }
                        }
                        else
                        {
                            if (dr["n" + arr[i].ToString() + ""].ToString().Trim() == "X")
                            {
                                ts_nlcuoituan += 1;
                            }
                            if (dr["n" + arr[i].ToString() + ""].ToString().Trim() == "X2")
                            {
                                ts_nlcuoituan += decimal.Parse("0.5");
                            }
                        }
                    }
                    ts_nlcuoituan += decimal.Parse(dr["ts_ntonhc"].ToString());
                    dr["ts_nlcuoituan"] = ts_nlcuoituan.ToString();
                }
            }
            return ds;
        }       
        public DataSet get_ngaynghibu(string s_mmyy)
        {
            string sql_t = "";
            string sql = "select ll.manv,0.0 as ts_ngaynghibu,";
            for (int i = 1; i <=31; i++)
            {
                sql_t += " n" + i.ToString() + ",";
            }
            sql_t = sql_t.Trim().Substring(0, sql_t.Trim().Length - 1);
            sql += sql_t;
            sql += " from " + user + ".chamcong_" + s_mmyy.Trim() + " cc inner join " + user + ".llnv ll on ll.manv=cc.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " inner join "+user+".max_lan ml on hd.lan=ml.lan";
            sql += " where hd.manv=ml.manv and hd.ngaynghi is null and cc.mmyy='" + s_mmyy.Trim() + "'";
            sql += " order by ll.manv";
            ds= objDAL.get_data(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {               
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    decimal ts_ngaynghi = 0;
                    for (int i = 1; i <=31; i++)
                    {
                        if (dr["n" + i.ToString() + ""].ToString().Trim().Length > 2)
                        {
                            string[] arr = dr["n" + i.ToString() + ""].ToString().Trim().Split('/');
                            if (arr[0].Trim() == "NB" || arr[1].Trim() == "NB")
                            {
                                ts_ngaynghi += 1;
                            }
                            if (arr[0].Trim() == "B2" || arr[1].Trim() == "B2")
                            {
                                ts_ngaynghi += decimal.Parse("0.5");
                            }
                        }
                        else
                        {
                            if (dr["n" + i.ToString() + ""].ToString().Trim() == "NB")
                            {
                                ts_ngaynghi += 1;
                            }
                            if (dr["n" + i.ToString() + ""].ToString().Trim() == "B2")
                            {
                                ts_ngaynghi += decimal.Parse("0.5");
                            }
                        }
                    }
                    dr["ts_ngaynghibu"] = ts_ngaynghi.ToString();
                }                                
            }
            return ds;
        }
        
        public DataSet get_ngaynghibuhc(string s_mmyy)
        {
            string sql_t = "";
            string sql = "select ll.manv,0.0 as ts_ngaynghibuhc,";
            for (int i = 1; i <= 31; i++)
            {
                sql_t += " n" + i.ToString() + ",";
            }
            sql_t = sql_t.Trim().Substring(0, sql_t.Trim().Length - 1);
            sql += sql_t;
            sql += " from " + user + ".chamcong_" + s_mmyy.Trim() + " cc inner join " + user + ".llnv ll on ll.manv=cc.manv ";
            sql += " inner join " + user + ".hopdong hd on hd.manv=ll.manv ";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null and cc.mmyy='" + s_mmyy.Trim() + "'";
            sql += " order by ll.manv";
            ds= objDAL.get_data(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    decimal ts_ngaynghihc = 0;
                    for (int i = 1; i <= 31; i++)
                    {
                        if (dr["n" + i.ToString() + ""].ToString().Trim().Length > 2)
                        {
                            string[] arr = dr["n" + i.ToString() + ""].ToString().Trim().Split('/');
                            if (arr[0].Trim() == "BU" || arr[1].Trim() == "BU")
                            {
                                ts_ngaynghihc += 1;
                            }
                            if (arr[0].Trim() == "U2" || arr[1].Trim() == "U2")
                            {
                                ts_ngaynghihc += decimal.Parse("0.5");
                            }
                        }
                        else
                        {
                            if (dr["n" + i.ToString() + ""].ToString().Trim() == "BU")
                            {
                                ts_ngaynghihc += 1;
                            }
                            if (dr["n" + i.ToString() + ""].ToString().Trim() == "U2")
                            {
                                ts_ngaynghihc += decimal.Parse("0.5");
                            }
                        }
                    }
                    dr["ts_ngaynghibuhc"] = ts_ngaynghihc.ToString();
                }
            }
            return ds;
        }
        public long get_capid(int m_ma)
        {
            return objDAL.get_capid(m_ma);
        }

        public bool upd_bieu01(long m_id, int m_ma, string m_ngay, int m_c01, int m_c02, int m_c03, int m_c04, int m_c05, int m_c06, int m_c07, int m_c08, int m_c09, int m_c10, int m_c11, int m_c12, int m_c13, int m_c14, int m_c15, int m_c16, int m_c17, int m_c18, int m_c19, int m_c20, int m_c21, int m_c22, int m_c23, int m_userid)
        {
            return objDAL.upd_bieu01(m_id, m_ma, m_ngay, m_c01, m_c02, m_c03, m_c04, m_c05, m_c06, m_c07, m_c08, m_c09, m_c10, m_c11, m_c12, m_c13, m_c14, m_c15, m_c16, m_c17, m_c18, m_c19, m_c20, m_c21, m_c22, m_c23, m_userid);
        }

        public long get_id_dm_maubaocao()
        {
            long i_id;
            try
            {
                sql = "select nvl(max(id),0) as id from " + user + ".dm_maubaocao";
                i_id = long.Parse(get_data(sql).Tables[0].Rows[0]["id"].ToString()) + 1;
            }
            catch { i_id = 1; }
            return i_id;
        }  
        //
        public bool upd_dm_mabaocao(long id, string maloai, string tenloai, string tenmau, string filereport, int istt)
        {
            return objDAL.upd_dm_mabaocao(id, maloai, tenloai, tenmau, filereport,istt, user);
        }
        //
        public DataSet get_chamcongngay(string tableName, string ngay, string maca, string madv, bool ccvra, string som,string user)
        {
            return DAL.get_chamcongngay(tableName, ngay, maca, madv, ccvra, som,user);
        }
        //Ham lay lich lam viec
        public DataSet get_lichlv()
        {
            sql = " select * from " + user + ".lichlv order by maca";
            return DAL.get_Data(sql);
        }
        public DataSet get_nhomcc()
        {
            sql = " select * from " + user + ".nhom_chamcong order by ma";
            return DAL.get_Data(sql);
        }
        public string get_manhom_lichlv(string maca)
        {
            sql = " select manhom from " + user + ".lichlv  where maca="+maca+" ";
            return DAL.get_Data(sql).Tables[0].Rows[0]["manhom"].ToString().Trim();
        }
        // Ham lay danh sach nhan vien bao cao
        public DataSet get_dsnhanvienbc(string bophan)
        {
            sql = " select nv.manv, nv.ho||' '||nv.ten as hoten from " + user + ".llnv nv";
            sql += " inner join " + user + ".hopdong hd on nv.manv=hd.manv";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null";
            if (bophan != "")
                sql += " and nv.madv='" + bophan.Trim() + "'";
            sql += " order by manv";
            return DAL.get_data(sql);

        }
        // Ham cham cong nhan vien theo thang
        public DataSet get_ccnhanvienthang(string mmyy, string maca, string madv, string manv,string user)
        {
            return DAL.get_ccnhanvienthang(mmyy, maca, madv, manv,user);
        }
        //lay ten lich lam viec
        public DataSet get_tenlichlv(string ma)
        {
            sql = " select tenca from " + user + ".lichlv where maca='" + ma.Trim() + "'";
            return DAL.get_Data(sql);
        }
        public DataSet get_nhanvienvang(string maca, string ngay, string mmyy, string tenca)
        {
            sql = " select '' as stt,a.manv,a.ho||' '||a.ten as hovaten,a.phai, d.ten as lydo,lydo as malydo,";
            sql += " '" + maca + "' as maca,'" + tenca + "' as tenca,'" + ngay + "'as ngay,'" + mmyy + "' as mmyy,0 as ktra ,cv.diengiai as tencv";
            sql += " ,case when c.maca <> '' then 'Đã cập nhật lý do nghỉ' else 'Chưa cập nhật lý do nghỉ' end as ghichu";
            sql += " from " + user + ".llnv a left join " + user + ".ccnghi_"+mmyy+" c on a.manv=c.manv";
            sql += " and to_timestamp(to_char(c.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy') and c.maca=1";
            sql += " left join " + user + ".dmlydo_phep d on c.lydo= d.ma  left join " + user + ".hopdong hd on hd.manv=a.manv left join " + user + ".dmcv cv on hd.macv=cv.macv";
            sql += " where  (c.ngay is null or to_timestamp(to_char(c.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
            sql += " >to_timestamp('" + ngay + "','dd/MM/yyyy'))";
            if (maca == "1")
            {
                sql += " and (a.manv not in (select manv from " + user + ".ccvao_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca = 1) ";
                sql += " and a.manv not in (select manv from " + user + ".ccvao_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca <> 1)";
                sql += " )";
            }
            if (maca != "1")
            {
                sql += " and ( a.manv not in (select manv from " + user + ".ccnghi_"+mmyy+" where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca = 1 and lydo <>'')";
                sql += " and a.manv not in (select manv from " + user + ".ccvao_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy'))";
                sql += " and a.manv not in (select manv from " + user + ".ccvao_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca = " + maca + " ) ";
                sql += " )";
                sql += " union";
                sql += " select '' as stt,a.manv,a.ho||' '||a.ten as hovaten,a.phai,d.ten as lydo,lydo as malydo";
                sql += " ,'" + maca + "' as maca,'" + tenca + "' as tenca,'" + ngay + "'as ngay,'" + mmyy + "' as mmyy,1 as ktra ,cv.diengiai as tencv ";
                sql += " ,case when (select tenca from " + user + ".ccra_" + mmyy + " cr left join " + user + ".lichlv lv on cr.maca=lv.maca";
                sql += " where to_timestamp(to_char(cr.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy') and manv=a.manv) <>''";
                sql += "  then 'Đã chấm công ra ca'||' '|| lower((select tenca from " + user + ".ccra_" + mmyy + " cr left join " + user + ".lichlv lv on cr.maca=lv.maca ";
                sql += " where to_timestamp(to_char(cr.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy') and manv=a.manv))";
                sql += " else 'Đã chấm công vào ca'||' '|| lower((select tenca from " + user + ".ccvao_" + mmyy + " cv left join " + user + ".lichlv lv on cv.maca=lv.maca";
                sql += " where to_timestamp(to_char(cv.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy')";
                sql += " and manv=a.manv )) end as ghichu";
                sql += " from " + user + ".llnv a left join " + user + ".ccnghi_"+mmyy+" c on a.manv=c.manv";
                sql += " and to_timestamp(to_char(c.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy')";
                sql += " left join " + user + ".dmlydo_phep d on c.lydo= d.ma left join " + user + ".hopdong hd on hd.manv=a.manv left join " + user + ".dmcv cv on hd.macv=cv.macv";
                sql += " where (";
                sql += " c.ngay is null or to_timestamp(to_char(c.ngay,'dd/MM/yyyy'),'dd/MM/yyyy') >to_timestamp('" + ngay + "','dd/MM/yyyy'))";
                sql += " and a.manv in (select manv from " + user + ".ccra_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca <> 1 and maca <>" + maca + " )";
                sql += " or a.manv in (select manv from " + user + ".ccvao_" + mmyy + " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                sql += " =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca <> 1 and maca <>" + maca + " )";
            }
            return DAL.get_data(sql);
        }
        public string ngaycuoituan(string month, string year,int ngay_cc)
        {
            string ngay = "";
            try
            {                
                int nam = Convert.ToInt32(year);
                int thang = Convert.ToInt32(month);
                DateTime d1;
                int songay = DateTime.DaysInMonth(nam, thang);
                for (int i = 1; i <= ngay_cc; i++)
                {
                    d1 = new DateTime(nam, thang, i);
                    if (d1.DayOfWeek == DayOfWeek.Sunday || d1.DayOfWeek == DayOfWeek.Saturday)
                        ngay += i.ToString().Trim()+",";
                }                
            }
            catch
            {
                
            }
            return ngay;
        }
        public int cuoituan(string month, string year)
        {
            try
            {
                int ngay = 0;
                int nam = Convert.ToInt32(year);
                int thang = Convert.ToInt32(month);
                DateTime d1;
                int songay = DateTime.DaysInMonth(nam, thang);
                for (int i = 1; i <= songay; i++)
                {
                    d1 = new DateTime(nam, thang, i);
                    if (d1.DayOfWeek == DayOfWeek.Sunday || d1.DayOfWeek == DayOfWeek.Saturday)
                        ngay++;
                }
                return ngay;
            }
            catch
            {
                return 0;
            }
        }
        // so ngay thu 7 trong thang
        public int ngaythu7(string month, string year)
        {
            try
            {
                int ngay = 0;
                int nam = Convert.ToInt32(year);
                int thang = Convert.ToInt32(month);
                DateTime d1;
                int songay = DateTime.DaysInMonth(nam, thang);
                for (int i = 1; i <= songay; i++)
                {
                    d1 = new DateTime(nam, thang, i);
                    if (d1.DayOfWeek == DayOfWeek.Saturday)
                        ngay++;
                }
                return ngay;
            }
            catch
            {
                return 0;
            }
        }
        // tinh so ngay trong thang
        public int ngaytrongthang(string month, string year)
        {
            try
            {
                int songay = 0;
                int nam = Convert.ToInt32(year);
                int thang = Convert.ToInt32(month);
                DateTime d1;
                songay = DateTime.DaysInMonth(nam, thang);
                return songay;
            }
            catch
            {
                return 0;
            }
        }
        //lay du lieu bao cao tong hop cham cong theo thang
        public DataSet get_thchamcongthang(string mmyy, string maca, string madv)
        {
            return DAL.get_thchamcongthang(mmyy, maca, madv);
        }
        // lay du llieu danh sach nhan vien tham gia cham cong trong thang
        public DataSet get_dsnvchamcong(string mmyy, string maca, string madv)
        {
            return DAL.get_dsnvchamcong(mmyy, maca, madv);
        }
        // lay du lieu danh sach nhan vien cham cong
        public DataSet get_dsnvchamcongt(string mmyy, string maca, string madv)
        {
            return DAL.get_dsnvchamcongt(mmyy, maca, madv);
        }
        public DataSet get_tenlydophep(string ma)
        {
            sql = " select ten from " + user + ".dmlydo_phep where ma='" + ma.Trim() + "'";
            return DAL.get_Data(sql);
        }
        public DataSet get_lydophep()
        {
            sql = " select * from " + user + ".dmlydo_phep order by stt";
            return DAL.get_Data(sql);
        }
        public DataSet get_thbcchamcongthang(string mmyy, string maca, string madv, string ngay, string thang, string nam)
        {
            return DAL.get_thbcchamcongthang(mmyy, maca, madv, ngay, thang, nam);
        }
        public DataSet get_quydinhchamcong()
        {
            sql = " select * from " + user + ".dm_quydinhchamcong order by ma";
            return DAL.get_data(sql);
        }       
        //lay muc tru quy dinh
        public DataSet get_nhom_qdcc(string manhom)
        {
            sql = " select * from " + user + ".dm_quydinhchamcong where manhom='" + manhom + "' order by ma";
            return DAL.get_data(sql);
        }
        public DataSet getmaxid_quydinhchamcong()
        {
            sql = " select max(ma) as ma from " + user + ".dm_quydinhchamcong order by ma";
            return DAL.get_data(sql);
        }
        public bool upd_quydinhchamcong(int ma, string ten, int tu, int den, int solan, decimal songay, string kyhieu, int vao, int ra,string manhom)
        {
            return DAL.upd_quydinhchamcong(ma, ten, tu, den, solan, songay, kyhieu, vao, ra,manhom);
        }
        public bool stringtobool(string str)
        {
            bool flag = false;
            if (str.Trim() != "")
            {
                if (str.Trim() == "1")
                {
                    flag = true;
                }
                if (str.Trim() == "0")
                {
                    flag = false;
                }
            }
            return flag;
        }
        //Ta Thi Thuy Linh
        public bool Insert_thaydoi_bl(string manv, string mmyy, DateTime ngaygiubac, string bacluongcu, DateTime ngaynangbac, string bacluongmoi, string mangachcu, string mangachmoi, DateTime ngay, decimal mltt, int loainhap, string ghichu, string madv, string macv)
        {
            return objDAL.Insert_thaydoi_bl(manv, mmyy, ngaygiubac, bacluongcu, ngaynangbac, bacluongmoi, mangachcu, mangachmoi, ngay, mltt, loainhap, ghichu, madv, macv);
        }
        public bool Insert_thaydoi_pc(string manv, string mmyy, string loaipc, decimal hs_cu, DateTime ngayquydinh, decimal hs_moi, DateTime ngaygiu, DateTime ngay, decimal mltt, int loainhap, string ghichu, string madv, string macv)
        {
            return objDAL.Insert_thaydoi_pc(manv, mmyy, loaipc, hs_cu, ngayquydinh, hs_moi, ngaygiu, ngay, mltt, loainhap, ghichu, madv, macv);
        }
        //hàm remove duplicate bo  phan 
        public DataSet removeduplicate(DataSet ds_canhuy, string colname)
        {
            Hashtable hTable = new Hashtable();
            ArrayList duplicateList = new ArrayList();
            foreach (DataRow drow in ds_canhuy.Tables[0].Rows)
            {
                if (hTable.Contains(drow[""+colname+""]))
                    duplicateList.Add(drow);
                else
                    hTable.Add(drow[""+colname+""], string.Empty);
            }
            foreach (DataRow dRow in duplicateList)
                ds_canhuy.Tables[0].Rows.Remove(dRow);//dTable.Rows.Remove(dRow);           
            return ds_canhuy;
        }
        //them phan xu ly tien luong
        public bool get_tonghopcc(string mm, string yyyy)
        {
            bool tonghopcc = false;
            sql = " select tonghopcc from " + user_payroll + ".heso where mm='" + mm.Trim() + "' and yyyy='" + yyyy.Trim() + "'";
            DataSet ds = objDAL.getDataByCondition(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
                tonghopcc = bool.Parse(dr["tonghopcc"].ToString());
            return tonghopcc;
        }

        public bool upd_heso(string thang, string nam, decimal ltt, decimal ltt_pc, decimal thuong,
                decimal chunhat, decimal le, decimal d_thuong, decimal d_chunhat, decimal d_le, decimal bhxh, decimal bhtn, decimal om, decimal bhyt, decimal kpcd, decimal nghi,
                decimal hs1, decimal hs2, decimal hs3, decimal hs4, decimal chinh, decimal phu1, decimal phu2,
                decimal thuongtruc_ngay, decimal thuongtruc_cn, decimal thuongtruc_le,
                decimal hsuc_ngay, decimal hsuc_cn, decimal hsuc_le,
                decimal loaia, decimal loaib, decimal loaic, decimal loaid, decimal loaie,
                decimal a, decimal b, decimal c, decimal d, decimal e, string thuviec,
                string dihoc, decimal ngaylv, decimal db_c, decimal db_p, decimal db_k,
                decimal l1_c, decimal l1_p, decimal l1_k, decimal l2_c, decimal l2_p, decimal l2_k,
                decimal l3_c, decimal l3_p, decimal l3_k, decimal lg_tapsu, decimal lg_dihoc, decimal mtk_hs, bool tonghopcc)
        {
            return objDAL.upd_heso(thang, nam, ltt, ltt_pc, thuong,
                 chunhat, le, d_thuong, d_chunhat, d_le, bhxh, bhtn, om, bhyt, kpcd, nghi,
                 hs1, hs2, hs3, hs4, chinh, phu1, phu2,
                 thuongtruc_ngay, thuongtruc_cn, thuongtruc_le,
                 hsuc_ngay, hsuc_cn, hsuc_le,
                 loaia, loaib, loaic, loaid, loaie,
                 a, b, c, d, e, thuviec, dihoc, ngaylv,
                 db_c, db_p, db_k, l1_c, l1_p, l1_k, l2_c, l2_p, l2_k,
                 l3_c, l3_p, l3_k, lg_tapsu, lg_dihoc, mtk_hs, tonghopcc, user_payroll);
        }
        //
        public void Capnhat_Baohiem(decimal xhoi, decimal yte, decimal kpcd, decimal bhtn, string thangnam)
        {

            try
            {
                if (kpcd != (1 / 100))
                {
                    sql = " update " + user_payroll + ".bcc set ";
                    sql += " t_kpcd=(hs_lc+hs_lc*vk+hs_cv)*mltt*" + kpcd + "";
                    sql += " where  mmyy='" + thangnam + "'";
                    sql += " and manv in (select manv from " + user_payroll + ".nv where kpcd and mmyy='" + thangnam + "')";
                    objDAL.Upd_data(sql);
                }
                if (yte != (1 / 100))
                {
                    sql = " update " + user_payroll + ".bcc set ";
                    sql += " t_bhyt=(hs_lc+hs_lc*vk+hs_cv)*mltt*" + yte + "";
                    sql += " where mmyy='" + thangnam + "' and manv in (select manv from " + user_payroll + ".nv where bhyt and mmyy='" + thangnam + "')";
                    objDAL.Upd_data(sql);
                }

                if (bhtn != (1 / 100))
                {
                    sql = " update " + user_payroll + ".bcc set ";
                    sql += " t_bhtn=(hs_lc+hs_lc*vk+hs_cv)*mltt*" + bhtn + "";
                    sql += " where mmyy='" + thangnam + "' and manv in (select manv from " + user_payroll + ".nv where bhtn=1 and mmyy='" + thangnam + "')";
                    objDAL.Upd_data(sql);
                }
                if (xhoi != (5 / 100))
                {
                    sql = " update " + user_payroll + ".bcc set ";
                    sql += " t_bhxh=(hs_lc+hs_lc*vk+hs_cv)*mltt*" + xhoi + "";
                    sql += " where mmyy='" + thangnam + "' and manv in ";
                    sql += "(select manv from " + user_payroll + ".nv where bhxh and mmyy='" + thangnam + "')";
                    objDAL.Upd_data(sql);
                    sql = " update " + user_payroll + ".bcc set ";
                    sql += " t_bhxh=0";
                    sql += " where mmyy='" + thangnam + "' and manv in ";
                    sql += "  (select manv from " + user_payroll + ".nn where nghi_ts=26 and mmyy='" + thangnam + "')";
                    objDAL.Upd_data(sql);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public bool khoa_dulieu(string s_mmyy)
        {
            bool gttv = false;
            sql = "select khoadl from " + user + ".heso where mmyy='" +s_mmyy+ "'";
            DataTable dt_tmp = new DataTable();
            dt_tmp = objDAL.getDataByCondition(sql).Tables[0];
            foreach (DataRow r in dt_tmp.Rows)
            {
                gttv = (r["khoadl"]).ToString().Trim().Equals("1") ? true : false;
            }
            return gttv;
        }

        public bool upd_boiduong_pttt(string b_pttt, string b_loai, string b_mmyy, string b_tenloai, decimal b_ptv, decimal b_phu1,
       decimal b_phu2, decimal b_bsgayme, decimal b_ktvgayme, decimal b_hoisuc, decimal b_dungcu, decimal giupviec)
        {
            return objDAL.upd_boiduong_pttt(b_pttt, b_loai, b_mmyy, b_tenloai, b_ptv, b_phu1,
                b_phu2, b_bsgayme, b_ktvgayme, b_hoisuc, b_dungcu, giupviec, user);
        }
        public bool upd_hesobh(string loaibh, string loaihd, bool luongkhoan, bool hs_lc, bool hs_cv, bool hs_vk
            , bool hs_kv, bool hs_tn, bool hs_dh, string mmyy)
        {
            return objDAL.upd_hesobh(loaibh, loaihd, luongkhoan, hs_lc, hs_cv, hs_vk, hs_kv, hs_tn, hs_dh, mmyy,user);
        }
        // cap nhat thay doi he so tabc
        public bool upd_hesoc(string mmyy, string gbd_ccchieu, int cccuoituan,int cc_ngaycn, int ccthuongquy
            , int cctructc, int khoadl, int thcctudong, int sd_tgsomtre, int cctc_taikhoa, int tn_thcc, int dn_thcc, int sd_ccngayt7, int vantay, int ngachdn, int lanccvt, int luonghd, int hd_bantg, int luong_sp, decimal pc_tienan, decimal pc_tacatruc,
            string hs_vk,string hs_cv, string hs_db, string hs_dh,string hs_dt, string hs_kv, string hs_ld, string hs_th, string hs_tn, string hs_ud, string hs_kh, string hs_ta, string hs_tx,string hs_tdt, string hs_phepthem)
        {
            return objDAL.upd_hesoc(mmyy, gbd_ccchieu, cccuoituan,cc_ngaycn, ccthuongquy, cctructc, khoadl, thcctudong,sd_tgsomtre,cctc_taikhoa,tn_thcc,dn_thcc,sd_ccngayt7, vantay
                , ngachdn, lanccvt, luonghd,hd_bantg,luong_sp, pc_tienan,pc_tacatruc,hs_vk, hs_cv, hs_db,hs_dt, hs_dh, hs_kv, hs_ld, hs_th, hs_tn, hs_ud, hs_kh, hs_ta, hs_tx,hs_tdt, hs_phepthem, user);
        }

        // cap nhat thay doi he so tab_pc
        public bool upd_hesopc(string mmyy, decimal mltt, decimal mtietkiem_hs, decimal mltt_phucap, decimal lamviec, decimal snlvthucte, decimal mkpcd_hs, decimal mbhxh_hs, decimal mbhyt_hs, decimal mbhtn_hs, decimal sbhxh_hs
                , decimal sbhyt_hs, decimal sbhtn_hs, decimal mcaba_hs, decimal mcn_hs, decimal mletet_hs, decimal pcngnt_d, decimal pcngcn_d, decimal pcnglt_d
                , decimal hs1, decimal hs2, decimal hs3, decimal hs4, decimal ma, decimal mb, decimal mc, decimal thuonga, decimal thuongb, decimal thuongc, decimal pctt, decimal pctt1, decimal pctt2, decimal hpctt, decimal hpctt1, decimal hpctt2
                , decimal mom_hs, decimal thuviec, decimal dihoc, decimal pctq_nt, decimal pctq_cn, decimal pctq_lt, decimal pctc_nt, decimal pctc_cn, decimal pctc_lt, decimal hpctc_nt, decimal hpctc_cn, decimal hpctc_lt, decimal hsdst_abc, decimal stpc_abc, string h_abc, string h_tt, string h_ng, string h_tc, string h_tq, string gbdng_n, string gktng_n, string gbdng_d, string gktng_d)
        {
            return objDAL.upd_hesopc(mmyy, mltt, mtietkiem_hs, mltt_phucap, lamviec, snlvthucte, mkpcd_hs, mbhxh_hs, mbhyt_hs, mbhtn_hs, sbhxh_hs, sbhyt_hs, sbhtn_hs, mcaba_hs, mcn_hs
            , mletet_hs, pcngnt_d, pcngcn_d, pcnglt_d, hs1, hs2, hs3, hs4, ma, mb, mc, thuonga, thuongb, thuongc, pctt, pctt1, pctt2, hpctt, hpctt1, hpctt2, mom_hs, thuviec, dihoc, pctq_nt
            , pctq_cn, pctq_lt, pctc_nt, pctc_cn, pctc_lt, hpctc_nt, hpctc_cn, hpctc_lt,hsdst_abc,stpc_abc,h_abc,h_tt,h_ng,h_tc,h_tq,gbdng_n,gktng_n,gbdng_d,gktng_d, user);
        }
        //
        public DataSet bc_tonghopcocaucanbo(string ngay, string thang, string nam, string bophan)
        {
            sql = " select bp.madv,bp.tenbp,bp.sothutu as stt";
            sql += " , sum(case when mh.nhombc=1 then 1 else 0 end ) ts_bienche";
            sql += " , sum(case when mh.nhombc=2 then 1 else 0 end ) ts_nd68";
            sql += " , sum(case when dmcv.macv in ('001','003','005','007','011','012','013','014','015') then 1 else 0 end ) ql_truong";
            sql += " , sum(case when dmcv.macv in ('002','004','006','008','010') then 1 else 0 end ) ql_pho";
            sql += " ,sum(case when dmcv.macv in ('009') and dt.manhom in ('12') then 1 else 0 end) as nv_kt";
            sql += " ,sum(case when dmcv.macv in ('009') and dt.mactdt in ('001') then 1 else 0 end) as nv_vtlutru";
            sql += " ,sum(case when dmcv.macv in ('009') and dt.mactdt in ('038') then 1 else 0 end) as nv_lxe";
            sql += " ,sum(case when dmcv.macv in ('009') and dt.mactdt in ('064') then 1 else 0 end) as nv_phucvu";

            //theo trinh do sau dai hoc
            sql += " ,sum(case when l.mahocham not in ('2','3') and bc.mabc in ('TD') and   l.td_nnghiep in ('02')  then 1 else 0 end) as sdh_thS";
            sql += " ,sum(case when l.mahocham not in ('2','3') and bc.mabc in ('TD') and dt.manhom in ('01') and l.td_nnghiep in ('08') then 1 else 0 end) as sdh_bsck2";
            sql += " ,sum(case when l.mahocham not in ('2','3') and bc.mabc in ('TD') and dt.manhom in ('01') and l.td_nnghiep in ('07') then 1 else 0 end) as sdh_bsck1";
            sql += " ,sum(case when l.mahocham not in ('2','3')  and  bc.mabc in ('DH') and  dt.manhom in ('01') then 1 else 0 end) as dh_bsykhoa";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('02') and bc.mabc in ('DH')  then 1 else 0 end) as dh_duoc";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('06') and dt.mactdt in ('007') and bc.mabc in ('DH')  then 1 else 0 end) as dh_dieudg";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('08') and bc.mabc in ('DH')  then 1 else 0 end) as dh_hs";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('13') and dt.mactdt in ('051') and bc.mabc in ('DH')  then 1 else 0 end) as cn_tinhoc";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('12') and dt.mactdt in ('062') and bc.mabc in ('DH')  then 1 else 0 end) as cn_qtkd";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('13') and dt.mactdt in ('060') and bc.mabc in ('DH')  then 1 else 0 end) as cn_sp";
            sql += " ,0 as cn_ktyh";

            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('06') and dt.mactdt in ('007') and bc.mabc in ('CD')  then 1 else 0 end) as cd_dieudg";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('08') and bc.mabc in ('CD')  then 1 else 0 end) as cd_hs";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('13') and dt.mactdt in ('051') and bc.mabc in ('CD')  then 1 else 0 end) as cd_tinhoc";
            sql += " ,0 as cn_cdktyh";

            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('07') and bc.mabc in ('TH') then 1 else 0 end) as th_ys";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('07') and dt.mactdt in ('063') and bc.mabc in ('TH') then 1 else 0 end) as th_ysyhct";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('06') and dt.mactdt in ('007') and bc.mabc in ('TH')  then 1 else 0 end) as th_dieudg";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('04') and bc.mabc in ('TH') then 1 else 0 end) as th_ktvth";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('08') and bc.mabc in ('TH')  then 1 else 0 end) as th_hs";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('02') and bc.mabc in ('TH')  then 1 else 0 end) as th_duoc";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('14') and bc.mabc in ('TH')  then 1 else 0 end) as th_ktv";
            sql += " ,sum(case when l.mahocham not in ('2','3') and bc.mabc in ('TH') and dt.manhom not in('07','06','04','08','02','14')  then 1 else 0 end) as th_khac";

            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('03') and bc.mabc in ('SH')  then 1 else 0 end) as sh_duocta";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('06') and dt.mactdt in ('006','034') and bc.mabc in ('SH')  then 1 else 0 end) as sh_yta";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom not in ('06','03') and bc.mabc in ('SH')  then 1 else 0 end) as sh_k";
            sql += " ,sum(case when l.mahocham not in ('2','3') and dt.manhom in ('09') and dt.mactdt in ('012') then 1 else 0 end) as holy";
            sql += "  from nhansu.llnv as l left join nhansu.dmcd as cd on l.macd=cd.ma";
            sql += " inner join nhansu.hopdong as hd on l.manv=hd.manv left join nhansu.bangcap as bc on l.td_nnghiep=bc.mabangcap";
            sql += " left join nhansu.mahd mh on hd.loaihd=mh.mahd left join nhansu.bophan bp on l.madv=bp.madv";
            sql += " left join nhansu.dmcv on dmcv.macv=hd.macv";
            sql += " inner join nhansu.max_lan ml on hd.manv=ml.manv left join nhansu.ctdaotao dt on l.nnghiep=dt.mactdt";
            sql += " where (hd.ngaynghi is null or to_timestamp(hd.ngaynghi,'yyyy-mm-dd') > to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd' ))";
            sql += " and to_timestamp(hd.ngaydenct,'yyyy-mm-dd')<= to_timestamp('" + nam + "-" + thang + "-" + ngay + "','yyyy-mm-dd') and hd.lan=ml.lan ";
            if (bophan != "")
                sql += " and l.madv='" + bophan.Trim() + "'";
            sql += " group by bp.madv,bp.tenbp,bp.sothutu";
            sql += " order by bp.sothutu";
            return objDAL.get_data(sql);
        }
        //luong
        
        public bool upd_thuetncn(string manv, string mmyy, string madv, decimal tongtnbv, decimal thuong, decimal tnkhac, decimal t_banthan, decimal t_giacanh, decimal trukhac, decimal tyle, decimal giamthue, decimal t_thuetncn, decimal danop, string ghichu)
        {
            try
            {
                string sql = " update " + user + ".thnh set madv='" + madv.ToString() + "' ,tongtnbv=" + tongtnbv.ToString() + " ";
                sql += " ,thuong=" + thuong.ToString() + ",tnkhac=" + tnkhac.ToString() + "";
                sql += " ,t_banthan=" + t_banthan.ToString() + ",t_giacanh=" + t_giacanh.ToString() + "";
                sql += " ,trukhac=" + trukhac.ToString() + ",tyle=" + tyle.ToString() + ",t_thuetncn=" + t_thuetncn.ToString() + "";
                sql += " ,danop=" + danop.ToString() + ",ghichu='" + ghichu.ToString() + "'";
                sql += " where manv='" + manv + "' and mmyy='" + mmyy.ToString().Trim() + "'";
                objDAL.getDataByCondition(sql);

            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool upd_thueTNV(int m_id, decimal m_gh_duoi, decimal m_gh_tren, decimal m_ty_le, string s_mmyy)
        {
            return objDAL.upd_thueTNV(m_id, m_gh_duoi, m_gh_tren, m_ty_le, s_mmyy,user);
        }
        //tung them 26/06/2011
        public DataSet ds_chamcong_hc(string s_mmyy, string madvi)
        {
            DataSet ds = new DataSet();
            DataSet ads = new DataSet();
            try
            {
                sql = "select distinct 0 as stt,a.manv,a.madv,(a.ho||' '||a.ten) as hoten,";
                sql += " e.diengiai as chucdanh,b.mmyy as thangnam, ";
                sql += " case when b.sn_cong=0 then null else round(b.sn_cong,0) end as sn_cong,";
                sql += " case when b.nghi_om=0 then null else round(b.nghi_om,0) end as nghi_om,";
                sql += " case when b.nghi_conom=0 then null else round(b.nghi_conom,0) end as nghi_conom,";
                sql += " case when b.nghi_ad=0 then null else round(b.nghi_ad,0) end as nghi_ad,";
                sql += " case when b.nghi_nb=0 then null else round(b.nghi_nb,0) end as nghi_nb,";
                sql += " case when b.nghi_pn=0 then null else round(b.nghi_pn,0) end as nghi_pn,";
                sql += " case when b.nghi_ts=0 then null else round(b.nghi_ts,0) end as nghi_ts,";
                sql += " case when b.nghi_kt=0 then null else round(b.nghi_kt,0) end as nghi_kt,";
                sql += " case when b.nghi_td=0 then null else round(b.nghi_td,0) end as nghi_td,";
                sql += " case when b.nghi_ho=0 then null else round(b.nghi_ho,0) end as nghi_ho,";
                sql += " case when b.nghi_ct=0 then null else round(b.nghi_ct,0) end as nghi_ct,";
                sql += " case when b.nghi_ld=0 then null else round(b.nghi_ld,0) end as nghi_ld,";
                sql += " case when b.nghi_dh=0 then null else round(b.nghi_dh,0) end as nghi_dh,";
                sql += " case when b.nghi_ot=0 then null else round(b.nghi_ot,0) end as nghi_ot,";
                sql += " case when b.nghi_kl=0 then null else round(b.nghi_kl,0) end as nghi_kl,";
                sql += " case when b.nghi_kh=0 then null else round(b.nghi_kh,0) end as nghi_kh,";
                sql += " case when b.nghi_bl=0 then null else round(b.nghi_bl,0) end as nghi_bl,";
                sql += " case when b.ts_coluong=0 then null else round(b.ts_coluong,0) end as ts_coluong,";
                sql += " case when b.ts_kluong=0 then null else round(b.ts_kluong,0) end as ts_kluong,";
                sql += " case when b.ts_bhtra=0 then null else round(b.ts_bhtra,0) end as ts_bhtra,";
                sql += " case when b.ts_kha=0 then null else round(b.ts_kha,0) end asts_kha,";
                sql += " from " + user + ".llnv a ";
                sql += " join " + user + ".th_chamcong b on a.manv=b.manv ";
                sql += " join " + user + ".bophan c on a.madv=c.madv ";
                sql += " join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " join " + user + ".dmcv e on e.macv =d.macv ";
                
                sql += " where a.madv='" + madvi + "' ";
                ds = objDAL.getDataByCondition(sql);
                return ds;
            }
            catch
            {
                return null;
            }
        }
        //thanh tung
        public bool update_chamcong_hc(string manv, string madv, string mmyy, decimal sn_cong, decimal nghi_om, decimal nghi_conom, decimal nghi_ad, decimal nghi_nb,
            decimal nghi_pn, decimal nghi_ts, decimal nghi_kt, decimal nghi_td, decimal nghi_ho, decimal nghi_ct, decimal nghi_ld, decimal nghi_dh, decimal nghi_ot, decimal nghi_kl,
            decimal nghi_kh, decimal nghi_bl,decimal nghi_le,decimal nghi_bu, decimal ts_coluong, decimal ts_kluong, decimal ts_bhtra, decimal ts_khac,decimal ts_ngaycong,decimal ts_ngaytru)
        {
            return objDAL.update_chamcong_hc(manv, madv, mmyy, sn_cong, nghi_om, nghi_conom,
               nghi_ad, nghi_nb, nghi_pn, nghi_ts, nghi_kt, nghi_td, nghi_ho, nghi_ct, nghi_ld, nghi_dh, nghi_ot, nghi_kl, nghi_kh, nghi_bl,nghi_le,nghi_bu, ts_coluong, ts_kluong, ts_bhtra, ts_khac,ts_ngaycong,ts_ngaytru, user);
        }
        public DataSet load_chamcong_hc(string s_mmyy, string madvi)
        {
            DataSet ds = new DataSet();
            DataSet ads = new DataSet();
            string sql = "";
            try
            {
                sql = " select distinct 0 as stt,a.manv,(a.ho||' '||a.ten) as hoten,";
                sql += " e.ten as chucdanh, ";
                sql += " case when b.sn_cong=0 then null else round(b.sn_cong,1) end as sn_cong,";
                sql += " case when b.nghi_om=0 then null else round(b.nghi_om,1) end as nghi_om,";
                sql += " case when b.nghi_conom=0 then null else round(b.nghi_conom,1) end as nghi_conom,";
                sql += " case when b.nghi_ad=0 then null else round(b.nghi_ad,1) end as nghi_ad,";
                sql += " case when b.nghi_nb=0 then null else round(b.nghi_nb,1) end as nghi_nb,";
                sql += " case when b.nghi_pn=0 then null else round(b.nghi_pn,1) end as nghi_pn,";
                sql += " case when b.nghi_ts=0 then null else round(b.nghi_ts,1) end as nghi_ts,";
                sql += " case when b.nghi_kt=0 then null else round(b.nghi_kt,1) end as nghi_kt,";
                sql += " case when b.nghi_td=0 then null else round(b.nghi_td,1) end as nghi_td,";
                sql += " case when b.nghi_ho=0 then null else round(b.nghi_ho,1) end as nghi_ho,";
                sql += " case when b.nghi_ct=0 then null else round(b.nghi_ct,1) end as nghi_ct,";
                sql += " case when b.nghi_ld=0 then null else round(b.nghi_ld,1) end as nghi_ld,";
                sql += " case when b.nghi_dh=0 then null else round(b.nghi_dh,1) end as nghi_dh,";
                sql += " case when b.nghi_ot=0 then null else round(b.nghi_ot,1) end as nghi_ot,";
                sql += " case when b.nghi_kl=0 then null else round(b.nghi_kl,1) end as nghi_kl,";
                sql += " case when b.nghi_kh=0 then null else round(b.nghi_kh,1) end as nghi_kh,";
                sql += " case when b.nghi_bl=0 then null else round(b.nghi_bl,1) end as nghi_bl,";
                sql += " case when b.ts_coluong=0 then null else round(b.ts_coluong,1) end as ts_coluong,";
                sql += " case when b.ts_kluong=0 then null else round(b.ts_kluong,1) end as ts_kluong,";
                sql += " case when b.ts_bhtra=0 then null else round(b.ts_bhtra,1) end as ts_bhtra,";
                sql += " case when b.ts_khac=0 then null else round(b.ts_khac,1) end as ts_khac,";
                sql += " case when b.ts_ngaycong=0 then null else round(b.ts_ngaycong,1) end as ts_ngaycong,";
                sql += " case when b.ts_ngaytru=0 then null else round(b.ts_ngaytru,1) end as ts_ngaytru,";
                sql += " case when b.ts_giocong=0 then null else round(b.ts_giocong,1) end as ts_giocong";
                sql += " from " + user + ".llnv a ";
                sql += " left join " + user + ".th_chamcong_"+s_mmyy+" b on a.manv=b.manv ";
                sql += " left join " + user + ".bophan c on a.madv=c.madv ";
                sql += " inner join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " left join " + user + ".dmcd e on e.ma =a.macd ";
                sql += " where a.madv='" + madvi + "' ";
                return ds = objDAL.getDataByCondition(sql);

            }
            catch
            {
                return null;
            }
        }
        //
        public DataSet load_chamcong_thuongtruc(string s_mmyy, string madvi)
        {
            DataSet ds_cc_tt = new DataSet();
            DataSet ads = new DataSet();
            string sql = "";
            try
            {
                sql = "select distinct 0 as stt,a.manv,(a.ho||' '||a.ten) as hoten,";
                sql += " e.ten as chucdanh, ";
                sql += " case when b.tt_ngaythuong=0 then null else round(b.tt_ngaythuong,0) end as tt,";
                sql += " case when b.tt_chunhat=0 then null else round(b.tt_chunhat,0) end as tn,";
                sql += " case when b.tt_letet=0 then null else round(b.tt_letet,0) end as tl,";
                sql += " case when b.htt_ngaythuong=0 then null else round(b.htt_ngaythuong,0) end as ht,";
                sql += " case when b.htt_chunhat=0 then null else round(b.htt_chunhat,0) end as hn,";
                sql += " case when b.htt_letet=0 then null else round(b.htt_letet,0) end as hl,sntru_tt";
                sql += " from " + user + ".llnv a inner join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " left join " + user + ".th_chamcong_"+s_mmyy+" b on a.manv=b.manv ";
                sql += " left join " + user + ".bophan c on a.madv=c.madv ";
                sql += " inner join "+user+".max_lan ml on d.manv=ml.manv";
                sql += " left join " + user + ".dmcd e on e.ma =a.macd ";
                sql += " where d.lan=ml.lan ";
                if (madvi.Trim() != "")
                    sql += " and a.madv='" + madvi + "' ";
                return ds_cc_tt = objDAL.getDataByCondition(sql);
            }
            catch
            {
                return null;
            }
        }
        //
        public DataSet load_chamcong_ngoaigio(string s_mmyy, string madvi)
        {
            DataSet ds_cc_nggio = new DataSet();
            DataSet ads = new DataSet();
            string sql = "";
            try
            {
                sql = "select distinct 0 as stt,a.manv,(a.ho||' '||a.ten) as hoten,";
                sql += " e.ten as chucdanh, ";
                sql += " case when b.nng_ngaythuong=0 then null else round(b.nng_ngaythuong,1) end as nng_ngaythuong,";
                sql += " case when b.nng_chunhat=0 then null else round(b.nng_chunhat,1) end as nng_chunhat,";
                sql += " case when b.nng_letet=0 then null else round(b.nng_letet,1) end as nng_letet,";
                sql += " case when b.dng_ngaythuong=0 then null else round(b.dng_ngaythuong,1) end as dng_ngaythuong,";
                sql += " case when b.dng_chunhat=0 then null else round(b.dng_chunhat,1) end as dng_chunhat,";
                sql += " case when b.dng_letet=0 then null else round(b.dng_letet,1) end as dng_letet";
                sql += " from " + user + ".llnv a ";
                sql += " left join " + user + ".th_chamcong_"+s_mmyy+" b on a.manv=b.manv ";
                sql += " left join " + user + ".bophan c on a.madv=c.madv ";
                sql += " left join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " left join " + user + ".dmcd e on e.ma =a.macd ";
                sql += " where a.madv='" + madvi + "' ";
                return ds_cc_nggio = objDAL.getDataByCondition(sql);

            }
            catch
            {
                return null;
            }
        }
        // thanh tung 04/07/2011
        public DataSet load_chamcong_thuongquy(string s_mmyy, string madvi)
        {
            DataSet ds_cc_tquy = new DataSet();
            DataSet ads = new DataSet();
            string sql = "";
            try
            {
                sql = "select distinct 0 as stt,a.manv,(a.ho||' '||a.ten) as hoten,";
                sql += " e.ten as chucdanh, ";
                sql += " case when b.tq_ngaythuong=0 then null else round(b.tq_ngaythuong,1) end as tq_ngaythuong,";
                sql += " case when b.tq_chunhat=0 then null else round(b.tq_chunhat,1) end as tq_chunhat,";
                sql += " case when b.tq_letet=0 then null else round(b.tq_letet,1) end as tq_letet";
                sql += " from " + user + ".llnv a ";
                sql += " left join " + user + ".th_chamcong_"+s_mmyy+" b on a.manv=b.manv ";
                sql += " left join " + user + ".bophan c on a.madv=c.madv ";
                sql += " left join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " left join " + user + ".dmcd e on e.ma =a.macd";
                sql += " where a.madv='" + madvi + "' ";
                return ds_cc_tquy = objDAL.getDataByCondition(sql);

            }
            catch
            {
                return null;
            }
        }
        //phan tien luong
        public bool kt_heso(string s_mmyy)
        {
            int count = -1;
            DataSet dataset1 = new DataSet();            
            sql = "select * from " + user + ".heso where mmyy='" + s_mmyy.Trim() + "'";
            try
            {
                count = get_data(sql).Tables[0].Rows.Count;
            }
            catch
            {
                return false;
            }
            return (count > 0);
        }
        public bool kt_thangnam(string t, string n)
        {
            int i = 0;
            string d = DateTime.Now.Year.ToString();
            try
            {
                i = int.Parse(t);
                i = int.Parse(n);
            }
            catch
            {
                MessageBox.Show("Tháng năm không hợp lệ!", Msg);
                return false;
            }
            if (int.Parse(t) < 1 || int.Parse(t) > 12)
            {
                MessageBox.Show("Tháng không hợp lệ !", Msg);
                return false;
            }
            if (int.Parse(n) < (int.Parse(d) - 1) || int.Parse(n) > (int.Parse(d) + 1))
            {
                MessageBox.Show("Năm không hợp lệ!", Msg);
                return false;
            }
            return true;
        }
        //
        public bool update_chamcong_thuongquy(string manv, string madv, string mmyy, decimal tq_ngaythuong, decimal tq_chunhat, decimal tq_letet)
        {
            return objDAL.update_chamcong_thuongquy(manv, madv, mmyy, tq_ngaythuong, tq_chunhat, tq_letet, user);
        }
        public bool update_chamcong_tangcuong(string manv, string madv, string mmyy, decimal tc_ngaythuong, decimal tc_chunhat, decimal tc_letet, decimal htc_ngaythuong, decimal htc_chunhat, decimal htc_letet,decimal sntru_tc)
        {
            return objDAL.update_chamcong_tangcuong(manv, madv, mmyy,tc_ngaythuong,tc_chunhat,tc_letet,htc_ngaythuong,htc_chunhat,htc_letet,sntru_tc, user);
        }
        public DataSet load_chamcong_tangcuong(string s_mmyy, string madvi)
        {
            DataSet ds_cc_tcuong = new DataSet();
            string sql = "";
            try
            {
                sql = "select distinct 0 as stt,a.manv,(a.ho||' '||a.ten) as hoten, ";
                sql += " e.ten as chucdanh, ";
                sql += " case when b.tc_ngaythuong=0 then null else round(b.tc_ngaythuong,1) end as tc_ngaythuong,";
                sql += " case when b.tc_chunhat=0 then null else round(b.tc_chunhat,1) end as tc_chunhat,";
                sql += " case when b.tc_letet=0 then null else round(b.tc_letet,1) end as tc_letet,";
                sql += " case when b.htc_ngaythuong=0 then null else round(b.htc_ngaythuong,1) end as htc_ngaythuong,";
                sql += " case when b.htc_chunhat=0 then null else round(b.htc_chunhat,1) end as htc_chunhat,";
                sql += " case when b.htc_letet=0 then null else round(b.htc_letet,1) end as htc_letet,";
                sql += " case when b.sntru_tc=0 then null else round(b.sntru_tc,1) end as sntru_tc";
                sql += " from " + user + ".llnv a ";
                sql += " left join " + user + ".th_chamcong_"+s_mmyy+" b on a.manv=b.manv ";
                sql += " left join " + user + ".bophan c on a.madv=c.madv ";
                sql += " left join " + user + ".hopdong d on a.manv=d.manv ";
                sql += " left join " + user + ".dmcd e on e.ma =a.macd  ";
                sql += " where a.madv='" + madvi + "' ";
                return ds_cc_tcuong = objDAL.getDataByCondition(sql);
            }
            catch
            {
                return null;
            }
        }
        public DataSet tonghop_chamcong_tt(string s_mmyy, string madvi)
        {
            DataSet ds_th_tt = new DataSet();
            string sql1 = "", sql2 = "", ngay = "", chamcong_tt = "", ngay_ht = "";
            for (int i = 1; i <= 31; i++)
            {
                ngay = "n" + i.ToString();
                chamcong_tt += " case when trim(upper(" + ngay + ")) <> '' then trim(upper(" + ngay + "))  else '' end as m" + ngay + ", ";
            }
            sql1 = " select '" + s_mmyy + "' as mmyy,hd.macv,a.manv,bp.madv,bp.tenbp as bophan,trim(a.ho)||' '||trim(a.ten) as hoten,c.ten,";
            chamcong_tt = chamcong_tt.Trim();
            sql1 += chamcong_tt.Substring(0, chamcong_tt.Length - 1);
            sql1 += " , 0 as tt,0 as tn,0 as tl,0 as ht,0 as hn,0 as hl,0 as sntru_tt,bp.sothutu as stt_bp,cv.stt as stt_cv,c.thutu as stt_cd";
            sql1 += " from " + user + ".llnv as a inner join " + user + ".hopdong as hd on a.manv=hd.manv ";
            sql1 += " inner join " + user + ".chamcong_thuongtruc_"+s_mmyy+" as b on a.manv=b.manv";
            sql1 += " left join " + user + ".dmcd as c on a.macd=c.ma";
            sql1 += " left join " + user + ".bophan as bp on bp.madv=b.madv";
            sql1 += " inner join " + user + ".dmcv as cv on hd.macv=cv.macv";
            sql1 += " inner join " + user + ".mahd mh on hd.loaihd=mh.mahd";
            sql1 += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql1 += " where hd.ngayhd is not null and b.mmyy='" + s_mmyy + "'";
            sql1 += "  and b.madv='" + madvi + "' ";
            sql1 += " and hd.lan=ml.lan";
            sql1 += " order by bp.sothutu";
            sql2 = " select * from " + user + ".dmlydo_phep where nhombc='TR' order by stt";
            ds_th_tt = objDAL.getDataByCondition(sql1, sql2);
            
            if (ds_th_tt.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds_th_tt.Tables[0].Rows)
                {
                    int tt = 0, tn = 0, tl = 0, ht = 0, hn = 0, hl = 0;
                    for (int i = 1; i <= 31; i++)
                    {
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "TT")
                            tt++;
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "TN")
                            tn++;
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "TL")
                            tl++;
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "HT")
                            ht++;
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "HN")
                            hn++;
                        if (dr["mn" + i.ToString() + ""].ToString().Trim().ToUpper() == "HL")
                            hl++;
                        string str = dr["mn" + i.ToString() + ""].ToString();
                        if (str.Trim() != "")
                        {
                            DataRow[] r = ds_th_tt.Tables[1].Select("ma = '" + str.Trim() + "' ");
                            if (r.Length > 0)
                            {
                                dr["mn" + i.ToString() + ""] = r[0]["kihieu_phep"].ToString();
                            }
                            else
                            {
                                dr["mn" + i.ToString() + ""] = "";
                            }
                        }
                        else
                        {
                            dr["mn" + i.ToString() + ""] = "";
                        }
                    }
                    dr["tt"] = tt.ToString();
                    dr["tn"] = tn.ToString();
                    dr["tl"] = tl.ToString();
                    dr["ht"] = ht.ToString();
                    dr["hn"] = hn.ToString();
                    dr["hl"] = hl.ToString();
                }
            }
            else
            {
                MessageBox.Show("Chua có dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return ds_th_tt;
        }
        public bool update_lydophep(DataSet ds_nvv, string mmyy, int loaicn)
        {
            return DAL.update_lydophep(ds_nvv, mmyy, loaicn,user);
        }
        public bool del_ngaycongvt(string manv, string mmyy,string maca,string ngay,bool vaora, int kt_ng)
        {
            return DAL.del_ngaycongvt(manv, mmyy, maca, ngay, vaora, kt_ng, user);
        }
        public DataSet get_nhanvienvang(string ngay, int loaicn, string maca, string madv, string mmyy, string tenca,string manhom,string str_maca)
        {
            sql = " select '' as stt, l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,l.phai,cv.diengiai as tencv,bp.tenbp,bp.sothutu";
            sql += " ,'" + maca + "' as maca,'"+tenca+"' as tenca,ld.ten as tenlydo,'0' as loaikt,'" + mmyy + "' as mmyy";
            sql += " ,'" + ngay + "' as ngay";
            if (loaicn == 0)//nv vang
            {
                if (manhom.Trim() == "HC")
                {
                    sql += " ,case  when cc.lydo <>'' then cc.lydo";
                    sql += " when (to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(ts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.dsnv_nghithaisan ts where nts.manv=ts.manv)";
                    sql += " and (case nts.denngay is not null when to_timestamp('" + ngay + "','dd/mm/yyyy') between to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')";
                    sql += " and to_timestamp(to_char(nts.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') then true else false end";
                    sql += " or case when (nts.denngay is null and to_timestamp(to_char(nts.tungay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                    sql += " < to_timestamp('" + ngay + "','dd/MM/yyyy')) then true else false end)) then 'TS'";
                    sql += " when (to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(q.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.qtdaotao q where qt.manv=q.manv)";
                    sql += " and (case qt.denngay is null  when to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + ngay + "','dd/mm/yyyy') then true";
                    sql += " else false end or case qt.denngay is not null when (to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') < to_timestamp('" + ngay + "','dd/mm/yyyy') and to_timestamp('" + ngay + "','dd/mm/yyyy') < to_timestamp(to_char(qt.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') )";
                    sql += " then true  else false end)) then 'DH'";
                    sql += " when (to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(c.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.congviec c where ct.manv=c.manv)";
                    sql += " and (case ct.denngay is null  when to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + ngay + "','dd/mm/yyyy') then true";
                    sql += " else false end or case ct.denngay is not null when (to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') < to_timestamp('" + ngay + "','dd/mm/yyyy') and to_timestamp('" + ngay + "','dd/mm/yyyy') < to_timestamp(to_char(ct.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') )";
                    sql += " then true  else false end)) then 'CT'";
                    sql += " else cc.lydo end malydo";
                }
                if (manhom.Trim() == "HN")
                {
                    sql += " ,case  when cc.lydo <>'' then cc.lydo";
                    sql += " when (to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(ts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.dsnv_nghithaisan ts where nts.manv=ts.manv)";
                    sql += " and (case nts.denngay is not null when to_timestamp('" + ngay + "','dd/mm/yyyy') between to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')";
                    sql += " and to_timestamp(to_char(nts.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') then true else false end";
                    sql += " or case when (nts.denngay is null and to_timestamp(to_char(nts.tungay,'dd/MM/yyyy'),'dd/MM/yyyy')";
                    sql += " < to_timestamp('" + ngay + "','dd/MM/yyyy')) then true else false end)) then 'S2'";
                    sql += " when (to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(q.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.qtdaotao q where qt.manv=q.manv)";
                    sql += " and (case qt.denngay is null  when to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + ngay + "','dd/mm/yyyy') then true";
                    sql += " else false end or case qt.denngay is not null when (to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') < to_timestamp('" + ngay + "','dd/mm/yyyy') and to_timestamp('" + ngay + "','dd/mm/yyyy') < to_timestamp(to_char(qt.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') )";
                    sql += " then true  else false end)) then 'D2'";
                    sql += " when (to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(c.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.congviec c where ct.manv=c.manv)";
                    sql += " and (case ct.denngay is null  when to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + ngay + "','dd/mm/yyyy') then true";
                    sql += " else false end or case ct.denngay is not null when (to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') < to_timestamp('" + ngay + "','dd/mm/yyyy') and to_timestamp('" + ngay + "','dd/mm/yyyy') < to_timestamp(to_char(ct.denngay,'dd/mm/yyyy'),'dd/mm/yyyy') )";
                    sql += " then true  else false end)) then 'T2'";
                    sql += " else cc.lydo end malydo";
                }
                if (manhom.Trim() == "NG" || manhom.Trim() == "TR" || manhom.Trim() == "TQ" || manhom.Trim() == "TC")
                {
                    sql += " ,'' as malydo";
                }
                sql += " ,'' as tg_chamcong,'' tg_somtre,false as somtre,'' as thoigian,0 as kt_ngv";
                sql += " ,'' as ngayr,'' as tg_chamcongr,'' tg_somtrer,false as somtrer,'' as thoigianr,0 as kt_ngr";
                sql += " ,case when cc.lydo <> '' then 'Đã cập nhật lý do' else 'Chưa cập nhật lý do' end as ghichu";
            }
            if (loaicn == 1)// nv di muon
            {
                sql += " ,cc.lydo as malydo,to_char(cc.thoigian,'hh24:mi') as tg_chamcong,cc.tgsomtre as tg_somtre,somtre as somtre,cc.thoigian as thoigian,cc.ghichu,cc.kt_ng as kt_ngv";
            }
            if (loaicn == 2)// nv ve som
            {
                sql += " ,cc.lydo as malydo,to_char(cc.thoigian,'hh24:mi') as tg_chamcong,cc.tgsomtre as tg_somtre,somtre as somtre,cc.thoigian as thoigian,cc.ghichu,cc.kt_ng as kt_ngv";
            }
            if (loaicn == 3)//nv chua cham cong ra
            {
                sql += " ,cc.lydo as malydo,'' as tg_chamcong,'' tg_somtre,false as somtre,'' as thoigian,cc.ghichu,cc.kt_ng as kt_ngv";
            }
                       
            sql += " from "+user+".llnv l left join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " inner join "+user+".dmcv cv on hd.macv=cv.macv";
            sql += " inner join "+user+".bophan bp on l.madv=bp.madv";
            if (loaicn == 0)//nv vang
            {
                sql += " left join "+user+".ccnghi_" + mmyy + " cc on l.manv=cc.manv and to_timestamp(to_char(cc.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy')";
                sql += " left join "+user+".dmlydo_phep ld on cc.lydo=ld.ma ";
                sql += " left join "+user+".dsnv_nghithaisan nts on l.manv=nts.manv and to_timestamp(to_char(nts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(ts.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.dsnv_nghithaisan ts where nts.manv=ts.manv)";
                sql += " left join "+user+".qtdaotao qt on l.manv=qt.manv and to_timestamp(to_char(qt.tungay,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(q.tungay,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.qtdaotao q where qt.manv=q.manv)";
                sql += " left join "+user+".congviec ct on l.manv=ct.manv and kt_ct=1 and to_timestamp(to_char(ct.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') =(select max(to_timestamp(to_char(c.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy')) from nhansu.congviec c where ct.manv=c.manv)";
            }
            if (loaicn == 1)//nv di muon
            {
                sql += " inner join "+user+".ccvao_" + mmyy + " cc on l.manv=cc.manv and to_timestamp(to_char(cc.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy')";
                sql += " left join "+user+".dmlydo_phep ld on cc.lydo=ld.ma";
            }
            if (loaicn == 2)//nv ve som
            {
                sql += " inner join "+user+".ccra_" + mmyy + " cc on l.manv=cc.manv and to_timestamp(to_char(cc.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy')";
                sql += " left join "+user+".dmlydo_phep ld on cc.lydo=ld.ma";
            }
            if (loaicn == 3)//nv chua cham cong ra
            {
                sql += " inner join "+user+".ccvao_" + mmyy + " cc on l.manv=cc.manv and to_timestamp(to_char(cc.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay + "','dd/MM/yyyy') and cc.maca=" + maca + "";
                sql += " left join "+user+".dmlydo_phep ld on cc.lydo=ld.ma";
            }
            sql += " where hd.lan=ml.lan and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaydenct,'dd/MM/yyyy'),'dd/MM/yyyy')";
            sql += " >to_timestamp('" + ngay + "','dd/MM/yyyy'))";
            if (loaicn == 0)//nv vang 
            {
                if (maca.Trim() != "1")
                {                    
                    sql += " and l.manv not in (select manv from " + user + ".ccvao_" + mmyy + " ";
                    sql += " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy') =to_timestamp('" + ngay + "','dd/MM/yyyy') ";
                    sql += " and to_char(maca) in (" + str_maca.Trim() + "))";
                    sql += " and l.manv not in (select manv from " + user + ".ccnghi_" + mmyy + " ";
                    sql += " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy') =to_timestamp('" + ngay + "','dd/MM/yyyy') and to_char(maca) in (" + str_maca.Trim() + "))";
                }
                else
                {
                    sql += " and l.manv not in (select manv from " + user + ".ccvao_" + mmyy + " ";
                    sql += " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy') =to_timestamp('" + ngay + "','dd/MM/yyyy') and to_char(maca) in (" + str_maca + "))"; //
                    sql += " and l.manv not in (select manv from " + user + ".ccnghi_" + mmyy + " ";
                    sql += " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy') =to_timestamp('" + ngay + "','dd/MM/yyyy') and to_char(maca) in (" + str_maca + "))";//
                }
            }
            if (loaicn == 1)//nv di muon
            {
                sql += " and cc.somtre=true ";
                sql += " and cc.maca=" + maca + "";
            }
            if (loaicn == 2)//nv ve som
            {
                sql += " and cc.somtre = true ";
                sql += " and cc.maca=" + maca + "";
            }
            if (loaicn == 3)//nv chua cham cong ra
            {
                sql += " and l.manv not in (select manv from "+user+".ccra_" + mmyy + " ";
                sql += " where to_timestamp(to_char(ngay,'dd/MM/yyyy'),'dd/MM/yyyy') =to_timestamp('" + ngay + "','dd/MM/yyyy') and maca = " + maca + ")";
            }
            if (madv.Trim() != "")
                sql += " and l.madv ='" + madv.Trim() + "'";
            sql += " order by bp.sothutu,cv.stt,l.manv";
            return DAL.get_data(sql);
        }
        public DataSet get_nvccngoaigio(string s_bophan, string maca, string tenca,string s_ncuoithang, string ngay,string s_mmyy,string ngay1,string s_mmyy1)
        {
            sql = " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,cd.ten as chucdanh,to_char(cv.maca) as maca,cv.ghichu";
            sql += " ,to_char(cv.thoigian,'hh24:mi') as giocc_v,to_char(cr.thoigian,'hh24:mi') as giocc_r";
            sql += " ,to_char(cv.thoigian,'dd/MM/yyyy') as ngaycc_v,to_char(cr.thoigian,'dd/MM/yyyy') as ngaycc_r";
            sql += " ,cv.thoigian as thoigian_v,cv.kt_ng as kt_ngv,cr.thoigian as thoigian_r,cr.kt_ng as kt_ngr";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".bophan bp on l.madv=bp.madv";
            sql += " left join " + user + ".ccvao_" + s_mmyy + " cv on l.manv=cv.manv";
            sql += " left join " + user + ".ccra_" + s_mmyy + " cr on l.manv=cr.manv";
            sql += " left join " + user + ".dmcd cd on l.macd=cd.ma";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng and cv.kt_ng <>0";
            sql += " and to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd') = to_timestamp('"+ngay+"','dd/mm/yyyy')";
            sql += " and to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd') = to_timestamp('" + ngay + "','dd/mm/yyyy')";
            sql += " and to_char(cv.maca)= '" + maca + "' ";
            if (s_bophan.Trim() != "")
                sql += " and l.madv = '" + s_bophan.Trim() + "'";
            sql += " union";
            sql += " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,cd.ten as chucdanh,to_char(cv.maca) as maca,cv.ghichu";
            sql += " ,to_char(cv.thoigian,'hh24:mi') as giocc_v,to_char(cr.thoigian,'hh24:mi') as giocc_r";
            sql += " ,to_char(cv.thoigian,'dd/MM/yyyy') as ngaycc_v,to_char(cr.thoigian,'dd/MM/yyyy') as ngaycc_r";
            sql += " ,cv.thoigian as thoigian_v,cv.kt_ng as kt_ngv,cr.thoigian as thoigian_r,cr.kt_ng as kt_ngr";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".bophan bp on l.madv=bp.madv";
            sql += " left join " + user + ".ccvao_" + s_mmyy + " cv on l.manv=cv.manv";
            sql += " left join " + user + ".ccra_" + s_mmyy + " cr on l.manv=cr.manv";
            sql += " left join " + user + ".dmcd cd on l.macd=cd.ma";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng and cv.kt_ng =0";
            sql += " and to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd') = to_timestamp('" + ngay + "','dd/mm/yyyy')";
            sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
            sql += " and to_char(cv.maca)= '" + maca + "' ";
            if (s_bophan.Trim() != "")
                sql += " and l.madv = '" + s_bophan.Trim() + "'";
            if (bMmyy(s_mmyy1) && ngay.Trim()== s_ncuoithang.Trim())
            {
                sql += " union";
                sql += " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,cd.ten as chucdanh,to_char(cv.maca) as maca,cv.ghichu";
                sql += " ,to_char(cv.thoigian,'hh24:mi') as giocc_v,to_char(cr.thoigian,'hh24:mi') as giocc_r";
                sql += " ,to_char(cv.thoigian,'dd/MM/yyyy') as ngaycc_v,to_char(cr.thoigian,'dd/MM/yyyy') as ngaycc_r";
                sql += " ,cv.thoigian as thoigian_v,cv.kt_ng as kt_ngv,cr.thoigian as thoigian_r,cr.kt_ng as kt_ngr";
                sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
                sql += " inner join " + user + ".bophan bp on l.madv=bp.madv";
                sql += " inner join " + user + ".ccvao_" + s_mmyy + " cv on l.manv=cv.manv";
                sql += " inner join " + user + ".ccra_" + s_mmyy1 + " cr on cv.manv=cr.manv";
                sql += " left join " + user + ".dmcd cd on l.macd=cd.ma";
                sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
                sql += " where hd.lan=ml.lan and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng and cv.kt_ng =0";
                sql += " and to_timestamp(to_char(cv.ngay,'dd/mm/yyyy'),'dd/mm/yyyy')=to_timestamp('" + s_ncuoithang.Trim() + "','dd/mm/yyyy')";
                sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
                sql += " and to_char(cv.maca)= '" + maca + "' ";
                if (s_bophan.Trim() != "")
                    sql += " and l.madv = '" + s_bophan.Trim() + "'";
            }
            return objDAL.get_data(sql);
        }
        //lay kt_ng lon nhat trong ngay
        public DataSet max_ktng(string manv, string maca, string ngay_cc, string mmyy)
        {
            sql = " select max(kt_ng) as kt_ng from "+user+".ccvao_" + mmyy + " cv";
            sql += " where cv.manv='"+manv.Trim()+"' and maca="+maca+"";
            sql += " and to_timestamp(to_char(cv.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + ngay_cc + "','dd/MM/yyyy')";
            return objDAL.get_data(sql);
        }
        public bool upd_ccngoaigio(string manv, string maca, string ngaycc, DateTime thoigian, int kt_ng,int kt_ngcu, string mmyy, string ghichu,bool vaora)
        {
            return objDAL.upd_ccngoaigio(manv, maca, ngaycc, thoigian, kt_ng,kt_ngcu, mmyy, ghichu,vaora, user);
        }
        public bool insert_ccngoaigio(string manv, string maca, string ngaycc, DateTime thoigian, int kt_ng, string mmyy, string ghichu,bool vaora)
        {
            return objDAL.insert_ccngoaigio(manv, maca, ngaycc, thoigian, kt_ng, mmyy, ghichu,vaora, user);
        }
        public bool del_ccngoaigio(string manv, string maca, string ngay, string mmyy, int kt_ng, bool vaora)
        {
            return objDAL.del_ccngoaigio(manv, maca, ngay, mmyy, kt_ng, vaora, user);
        }
        public DataSet get_qdngoaigio(string mmyy)
        {
            sql = " select gbdng_n,gbdng_d,gktng_n,gktng_d from "+user+".heso where mmyy='"+mmyy+"'";
            return objDAL.get_data(sql);            
        }
        public DataSet get_nvchamcong_vantaythang(string mmyy,string s_bophan,string nhomca,string tungay, string denngay)
        {
            sql = " select l.manv,l.madv,bp.sothutu,to_char(cv.ngay,'dd/mm/yyyy') as ngaylv ";
            sql += " ,to_char(cv.thoigian,'hh24:mi') as tg_v,to_char(cr.thoigian,'hh24:mi') as tg_ra,cv.lydo as lydo,cr.lydo as lydo_r";
            sql += " ,cv.somtre as somtre_v,cv.tgsomtre as tgsomtre_v";
            sql += " ,cr.somtre as somtre_r,cr.tgsomtre as tgsomtre_r,to_char(cv.maca) as maca,lv.manhom";
            sql += " from " + user + ".llnv l inner join " + user + ".ccvao_" + mmyy + " cv on l.manv=cv.manv";
            sql += " left join " + user + ".ccra_" + mmyy + " cr on l.manv=cr.manv";
            sql += " left join " + user + ".bophan bp on l.madv=bp.madv";
            sql += " inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".lichlv lv on cv.maca=lv.maca";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and (ngaynghi is null or to_timestamp(to_char(ngaynghi,'dd/mm/yyyy'),'dd/mm/yyyy') > to_timestamp('" + denngay + "','dd/mm/yyyy'))";
            sql += " and to_timestamp(to_char(cv.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') = to_timestamp(to_char(cr.ngay,'dd/mm/yyyy'),'dd/mm/yyyy')";
            sql += " and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng ";
            sql += " and to_timestamp(to_char(cv.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') >= to_timestamp('" + tungay + "','dd/mm/yyyy')";
            sql += " and to_timestamp(to_char(cv.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + denngay + "','dd/mm/yyyy')";
            if (nhomca.Trim() != "")
                sql += " and to_char(cv.maca) in (" + nhomca.Trim() + ")";
            if (s_bophan.Trim() != "")
                sql += " and l.madv='" + s_bophan.Trim() + "'";

            sql += " union select l.manv,l.madv,bp.sothutu,";
            sql += " to_char(cn.ngay,'dd/mm/yyyy') as ngaylv,'' as tg_v,'' as tg_ra,cn.lydo as lydo,'' as lydo_r,";
            sql += " false as somtre_v,'' as tgsomtre_v,";
            sql += " false as somtre_r,'' as tgsomtre_r, cn.maca,lv.manhom";
            sql += " from nhansu.llnv l left join nhansu.ccnghi_" + mmyy + " cn on l.manv=cn.manv and cn.mmyy='" + mmyy + "'";
            sql += " left join nhansu.bophan bp on l.madv=bp.madv";
            sql += " inner join nhansu.hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".lichlv lv on cn.maca=lv.maca";
            sql += " inner join nhansu.max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and (ngaynghi is null or to_timestamp(to_char(ngaynghi,'dd/mm/yyyy'),'dd/mm/yyyy') > ";
            sql += " to_timestamp('" + denngay.Trim() + "','dd/mm/yyyy')) and ngay is not null";
            sql += " and to_timestamp(to_char(cn.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') >= to_timestamp('" + tungay + "','dd/mm/yyyy')";
            sql += " and to_timestamp(to_char(cn.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') <= to_timestamp('" + denngay + "','dd/mm/yyyy')";
            if (nhomca.Trim() != "")
                sql += " and to_char(cn.maca) in (" + nhomca.Trim() + ")";
            if (s_bophan.Trim() != "")
                sql += " and l.madv='" + s_bophan.Trim() + "'";
            sql += " order by sothutu";
            return objDAL.get_data(sql);
        }
        public DataSet get_nvccthuongtruc_vt(string s_mmyy,string s_mmyy1,string s_bophan,string nhomca,string s_ncuoithang)
        {
            sql = " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,l.madv,bp.tenbp,bp.sothutu,cd.ten as chucdanh";
            sql += " ,cv.lydo as lydo_v,cr.lydo as lydo_r,to_char(cv.ngay,'dd/mm/yyyy') as ngaylv";
            sql += " ,cv.somtre as somtre_v,cv.tgsomtre as tgsomtre_v";
            sql += " ,cr.somtre as somtre_r,cr.tgsomtre as tgsomtre_r,to_char(cv.maca) as maca";
            sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + user + ".bophan bp on l.madv=bp.madv";
            sql += " left join " + user + ".ccvao_" + s_mmyy + " cv on l.manv=cv.manv";
            sql += " left join " + user + ".ccra_" + s_mmyy + " cr on l.manv=cr.manv";
            sql += " left join " + user + ".dmcd cd on l.macd=cd.ma";
            sql += " inner join " + user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng";
            sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
            if (nhomca.Trim() != "")
                sql += " and to_char(cv.maca) in (" + nhomca.Trim() + ")";
            if (s_bophan.Trim() != "")
                sql += " and l.madv = '" + s_bophan.Trim() + "'";
            if (bMmyy(s_mmyy1))
            {
                sql += " union";
                sql += " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten,l.madv,bp.tenbp,bp.sothutu,cd.ten as chucdanh";
                sql += " ,cv.lydo as lydo_v,cr.lydo as lydo_r,to_char(cv.ngay,'dd/mm/yyyy') as ngaylv";
                sql += " ,cv.somtre as somtre_v,cv.tgsomtre as tgsomtre_v";
                sql += " ,cr.somtre as somtre_r,cr.tgsomtre as tgsomtre_r,to_char(cv.maca) as maca";
                sql += " from " + user + ".llnv l inner join " + user + ".hopdong hd on l.manv=hd.manv inner join " + user + ".bophan bp on l.madv=bp.madv";
                sql += " inner join " + user + ".ccvao_" + s_mmyy + " cv on l.manv=cv.manv";
                sql += " inner join " + user + ".ccra_" + s_mmyy1 + " cr on cv.manv=cr.manv";
                sql += " left join " + user + ".dmcd cd on l.macd=cd.ma inner join " + user + ".max_lan ml on hd.manv=ml.manv";
                sql += " where hd.lan=ml.lan  and cv.maca=cr.maca and cv.kt_ng=cr.kt_ng";
                sql += " and to_timestamp(to_char(cv.ngay,'dd/MM/yyyy'),'dd/MM/yyyy')=to_timestamp('" + s_ncuoithang.Trim() + "','dd/MM/yyyy')";
                sql += " and date_part('day', age(to_timestamp(to_char(cr.ngay,'yyyy-mm-dd'),'yyyy-mm-dd'),to_timestamp(to_char(cv.ngay,'yyyy-mm-dd'),'yyyy-mm-dd')))=1";
                if (nhomca.Trim() != "")
                    sql += " and to_char(cv.maca) in (" + nhomca.Trim() + ")";
                if (nhomca.Trim() != "")
                    sql += " and to_char(cr.maca) in (" + nhomca.Trim() + ")";
                if (s_bophan.Trim() != "")
                    sql += " and l.madv = '" + s_bophan.Trim() + "'";
            }
            return objDAL.get_data(sql);
        }
        public bool upd_ccngoaigio(int id, string manv, string mmyy, string ngay, DateTime batdau, DateTime ketthuc, string ngaybatdau, string ngayketthuc, int ghichu, bool hscc, bool t7cn, bool letet, string congviec)
        {
            return objDAL.upd_ccngoaigio(id, manv,mmyy, ngay, batdau, ketthuc, ngaybatdau, ngayketthuc, ghichu, hscc, t7cn, letet, congviec, user);
        }
        public bool upd_ccthongquy(int id, string manv, string mmyy, string ngay, DateTime batdau, DateTime ketthuc, string ngaybatdau, string ngayketthuc, int ghichu, bool hscc, bool t7cn, bool letet, string congviec)
        {
            return objDAL.upd_ccthuongquy(id, manv, mmyy, ngay, batdau, ketthuc, ngaybatdau, ngayketthuc, ghichu, hscc, t7cn, letet, congviec, user);
        }
    }
}
